OOP Question - Inheritance & Interfaces/MustOverride

R

Raterus

I've got a good question about object-oriented programming, I hope I can explain it well enough. I'm creating a UserManager, which I'm going to inherit from so different applications can share common methods.

Public MustInherit Class UserManager
Public MustOverride Sub AddUser(usr as InternetUser)
End Class

Public Class InternetUser
Public username as string
End Class

--

Ok so now I need my specific application "Blah" to inherit from these methods. My PROBLEM comes with this InternetUser type. My new appliation is extending this InternetUser type as well, so this MustOverride method AddUser needs to be changed to accept a "BlahUser" now. Since a BlahUser IS A InternetUser, I would think I can do this somehow.

Public Class BlahUser : Inherits InternetUser

End Class

Public Class BlahUserManager : Inherits UserManager
'my problem is what can I do when it comes to overriding the Sub "AddUser", since I'm now using BlahUser's, I'm getting compiler errors

Public Overrides Sub AddUser(usr as BlahUser) '<-- getting a compiler error here, since signature doesn't match parent.

End Sub
End Class

How can I successfully override this sub, but change the signature of the sub? I'd really like to have either an Interface or MustOverride Subs/Methods. I know I can use shadows, but that really seems like a cop out way of doing things, and I'm throwing out polymorphism when I do it.

I hope this is clear enough!, perhaps I'm just way off the mark here about how to do something like this, but I would almost think this should be able to be done.

Thanks for any help!
--Michael
 
O

One Handed Man \( OHM - Terry Burns \)

Tye Overloads instead of overrides as they are different types ( BlahUsr and
InternetUser )

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


I've got a good question about object-oriented programming, I hope I can
explain it well enough. I'm creating a UserManager, which I'm going to
inherit from so different applications can share common methods.

Public MustInherit Class UserManager
Public MustOverride Sub AddUser(usr as InternetUser)
End Class

Public Class InternetUser
Public username as string
End Class

--

Ok so now I need my specific application "Blah" to inherit from these
methods. My PROBLEM comes with this InternetUser type. My new appliation
is extending this InternetUser type as well, so this MustOverride method
AddUser needs to be changed to accept a "BlahUser" now. Since a BlahUser IS
A InternetUser, I would think I can do this somehow.

Public Class BlahUser : Inherits InternetUser

End Class

Public Class BlahUserManager : Inherits UserManager
'my problem is what can I do when it comes to overriding the Sub
"AddUser", since I'm now using BlahUser's, I'm getting compiler errors

Public Overrides Sub AddUser(usr as BlahUser) '<-- getting a compiler
error here, since signature doesn't match parent.

End Sub
End Class

How can I successfully override this sub, but change the signature of the
sub? I'd really like to have either an Interface or MustOverride
Subs/Methods. I know I can use shadows, but that really seems like a cop
out way of doing things, and I'm throwing out polymorphism when I do it.

I hope this is clear enough!, perhaps I'm just way off the mark here about
how to do something like this, but I would almost think this should be able
to be done.

Thanks for any help!
--Michael
 
H

Herfried K. Wagner [MVP]

Raterus said:
Public MustInherit Class UserManager
Public MustOverride Sub AddUser(usr as InternetUser)
End Class

Public Class InternetUser
Public username as string
End Class

--

Ok so now I need my specific application "Blah" to inherit from these
methods. My >PROBLEM comes with this InternetUser type. My new appliation
is extending this >InternetUser type as well, so this MustOverride method
AddUser needs to be changed to >accept a "BlahUser" now. Since a BlahUser
IS A InternetUser, I would think I can do this >somehow.

Public Class BlahUser : Inherits InternetUser

End Class

Public Class BlahUserManager : Inherits UserManager
'my problem is what can I do when it comes to
overriding the Sub "AddUser", since I'm now
using BlahUser's, I'm getting compiler errors

Public Overrides Sub AddUser(usr as BlahUser) '<-- getting a compiler
error here, since >signature doesn't match parent.

End Sub
End Class

\\\
Public Class InternetUser
'
End Class

Public Class BlaUser
Inherits InternetUser
'
End Class

Public MustInherit Class UserManager
Public MustOverride Sub AddUser(ByVal usr As InternetUser)
End Class

Public Class BlahUserManager
Inherits UserManager

Public Overloads Sub AddUser(ByVal usr As BlaUser)
'
End Sub

Public Overloads Overrides Sub AddUser(ByVal usr As InternetUser)
'
End Sub
End Class
///
 
R

Raterus

thanks, this is what I needed!
Herfried K. Wagner said:
\\\
Public Class InternetUser
'
End Class

Public Class BlaUser
Inherits InternetUser
'
End Class

Public MustInherit Class UserManager
Public MustOverride Sub AddUser(ByVal usr As InternetUser)
End Class

Public Class BlahUserManager
Inherits UserManager

Public Overloads Sub AddUser(ByVal usr As BlaUser)
'
End Sub

Public Overloads Overrides Sub AddUser(ByVal usr As InternetUser)
'
End Sub
End Class
///
 
C

CJ Taylor

Another Option would be to use an Inteface

That way its complete abstract and you can put in whatever you want...

-CJ

I had even thought to do that, but never went as far as to try, thanks!
 
R

Raterus

Can you give me some more details on implementing this? I know how to write an interface, but I'm getting confused when it comes to implementing and interface that has different return types than the interface.

Thanks,
 
C

CJ Taylor

It's really not much different than what your doing now... except you define
your interface such that

Public Interface MySampleInterface

... Methods , Properties, Events

End Interface

Public Class MyInternetUser
Implements MySampleInterface

.blah

End class

Finally the kicker

Public MustInherit Class UserManager

Public Overridable Sub AddUser(IUser as MySampleInterface)

end class

Forgive the poor naming conventions used, I'm just typing it in, not
actually testing it as I go...

You could do the usermanager as an interface as well if you so desired to
give it even more abstraction.

Such that

Public Interface IUserManager

Sub AddUser (IUser as MySampleInterface)

End Interface



Can you give me some more details on implementing this? I know how to write
an interface, but I'm getting confused when it comes to implementing and
interface that has different return types than the interface.

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top