Suppose I have two classes Base and Derived :
public class Base { public Base() { } public void methodA() { System.out.println("Base: methodA"); methodB(); } public void methodB() { System.out.println("Base: methodB"); } } public class Derived extends Base { public Derived() { } public void methodA() { super.methodA(); System.out.println("Derived: methodA"); } public void methodB() { System.out.println("Derived: methodB"); } }
Now with this:
Base d = new Derived(); d.methodA();
It will be printed:
Base: methodA Derived: methodB Derived: methodA
My question is: Is it possible to force d.methodA() use Base.methodB() ? I want the code to print:
Base: methodA Base: methodB Derived: methodA
For those who are known in C ++, this can be done using Base::methodB() in the Base class. Is there an equivalent in Java?
I am pretty sure that this was asked before, but I could not find anything, sorry if this is a duplicate.
java inheritance
yizzlez
source share