[ACCEPTED]-From Child instance call base class method that was overridden-inheritance
You don't; the subclass's method overrides 3 the superclass's method, by definition of 2 inheritance.
If you want the overridden method 1 to be available, expose it in the subclass, e.g.
Public Class Dog
Inherits Animal
Public Overrides Function Speak() As String
Return "Ruff"
End Function
Public Function SpeakAsAnimal() As String
Return MyBase.Speak()
End Function
End Class
I would ask why you are trying to get this 11 type of behavior. It seems to me that the 10 fact you need to invoke the parent class' implementation 9 of a method is an indication that you have 8 a design flaw somewhere else in the system.
Bottom 7 line though, as others have stated there 6 is no way to invoke the parent class' implementation 5 given the way you've structured your classes. Now 4 within the Dog class you could call
MyBase.Speak()
which 3 would invoke the parent class' implementation, but 2 from outside the Dog class there's no way 1 to do it.
I don't think you can.
The thing is that 15 the object is still a dog. the behavior 14 you're describing (getting "ruff" from the 13 casted object rather than "hello") is standard 12 because you want to be able to use the animal 11 class to let a bunch of different type of 10 animals speak.
For example, if you had a 9 third class as thus:
Public Class Cat
Inherits Animal
Public Overrides Function Speak() As String
Return "Meow"
End Function
End Class
Then you'd be able to 8 access them like thus:
protected sub Something
Dim oCat as New Cat
Dim oDog as New Dog
MakeSpeak(oCat)
MakeSpeak(oDog)
End sub
protected sub MakeSpeak(ani as animal)
Console.WriteLine(ani.Speak())
end sub
What you're talking 7 about doing basically breaks the inheritance 6 chain. Now, this can be done, by setting 5 up the Speak function to accept a parameter which 4 tells it to return it's base value or not 3 or a separate SPEAK function for the base 2 value, but out of the box, you're not going 1 to get things that behave this way.
I think if you drop "Overridable" and change 2 "Overrides" to "New" you'll get what you 1 want.
Public Class Animal
Public Function Speak() As String
Return "Hello"
End Function
End Class
Public Class Dog
Inherits Animal
Public New Function Speak() As String
Return "Ruff"
End Function
End Class
Dim dog As New Dog
Dim animal As Animal
dog.Speak() ' should be "Ruff"
animal = CType(dog, Animal)
animal.Speak() ' should be "Hello"
I know this has been posted a few months 9 back, but I'm still going to try and reply, maybe 8 just for the sake of completeness.
You've 7 been told that you can access the overriden 6 method from within the dog
class, and that you can 5 then expose it with a different name. But 4 what about using a conditional?
You can just 3 do:
Public Class Animal
Public Overridable Function Speak(Optional ByVal speakNormal as Boolean = False) As String
Return "Hello"
End Function
End Class
Public Class Dog
Inherits Animal
Public Overrides Function Speak(Optional ByVal speakNormal as Boolean = False) As String
If speakNormal then
return MyBase.Speak()
Else
Return "Ruff"
End If
End Function
End Class
And then call them like:
Dim dog As New Dog
Dim animal As new Animal
animal.Speak() //"Hello"
dog.Speak()//"Ruff"
dog.Speak(true)//"Hello"
Alternatively, you 2 can getTheAnimalInTheDog
and make it Speak()
:
You can just do:
Public Class Animal
Public Overridable Function Speak() As String
Return "Hello"
End Function
Public MustOverride Function GetTheAnimalInMe() As Animal
End Class
Public Class Dog
Inherits Animal
Public Overrides Function Speak() As String
Return "Ruff"
End Function
Public Overrides Function GetTheAnimalInMe() As Animal
Dim a As New Animal
//Load a with the necessary custom parameters (if any)
Return a
End Function
End Class
And then 1 again:
Dim dog As New Dog
Dim animal As new Animal
animal.Speak() //"Hello"
dog.Speak()//"Ruff"
dog.GetTheAnimalInMe().Speak()//"Hello"
Hope it helps ;)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.