Consider the following code:
Public Class Animal
Public Overridable Function Speak() As String
Return "Hello"
End Function
End Class
Public Class Dog
Inherits Animal
Public Overrides Function Speak() As String
Return "Ruff"
End Function
End Class
Dim dog As New Dog
Dim animal As Animal
animal = CType(dog, Animal)
// Want "Hello", getting "Ruff"
animal.Speak()
How can I convert/ctype the instance of Dog to Animal and have Animal.Speak get called?
Now we will create a Derived class that will extend this Base class i.e. In this class we will override the display() function i.e. Now if we create a Derived class object and call this overridden display() function from it , then due to dynamic binding in Java, always derived class display() method will be called i.e.
A child class can access the data members of its specific base class, i.e., variables and methods. Within this guide, we will be discussing different ways to execute or call the base call function in C++.
This can generally be achieved by two ways. Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly.
1 Answer. To explain: Scope resolution operator :: can be used to access the base class method even if overridden. To access those, first base class name should be written followed by the scope resolution operator and then the method name.
You don't; the subclass's method overrides the superclass's method, by definition of inheritance.
If you want the overridden method 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 type of behavior. It seems to me that the fact you need to invoke the parent class' implementation of a method is an indication that you have a design flaw somewhere else in the system.
Bottom line though, as others have stated there is no way to invoke the parent class' implementation given the way you've structured your classes. Now within the Dog class you could call
MyBase.Speak()
which would invoke the parent class' implementation, but from outside the Dog class there's no way to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With