Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From Child instance call base class method that was overridden

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?

like image 355
MattH Avatar asked Oct 28 '08 21:10

MattH


People also ask

How do you call a base class overridden method?

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.

Can a child class call base class methods?

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++.

How do you call base class overridden in Python?

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.

How can we access the overridden method of base class from the derived class in Java?

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.


2 Answers

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
like image 81
Steven A. Lowe Avatar answered Oct 12 '22 13:10

Steven A. Lowe


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.

like image 45
Mike Deck Avatar answered Oct 12 '22 13:10

Mike Deck