T
T
Tarasov Konstantin2014-07-13 14:12:50
Java
Tarasov Konstantin, 2014-07-13 14:12:50

Does polymorphism work when calling methods inside a superclass?

The question is something like this, is there a superclass

public class SuperClass {
//...
public void method0(){ /*Назовем этот блок кода А*/}
public void method1(){ this.method0() };
//...
}

And the class inherited from it
public class SubClass extends SuperClass {
//...
@Override
public void method0(){ /*А этот Б*/}
//...
}

What code will be executed as a result of the following call:
SuperClass ob = new SubClass();
ob.method1();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2014-07-13
@lexxpavlov

It would be faster to test it yourself by putting the output to the console in the method0 () method in the superclass and in the subclass.
Must call block B because it is a virtual method and the code version is determined by the real class of the object. Unless java has a special syntax with the this keyword.
Check it out for yourself, it will take a couple of minutes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question