I have a interesting scenario that.
public class Base {
public void hello(){
this.say();
System.out.println("hello-Base");
}
protected void say(){
System.out.println("say-Base");
}
}
public class Derived extends Base {
public Derived(){
super.hello();
}
public static void main(String[] args) {
Derived d = new Derived();
}
public void say(){
System.out.println("say-Derived");
}
}
The output given is that:
say-Derived
hello-Base
I was expecting that when super.hello() method was invoked, say() method of Base classes was invoked rather than say()method of Derived class.
What is the reason behind this logic?
Super.hello() calls this.say() which(this) refer to the current object i.e. derived so it calls the say method of derived class.