I have abstract class Shape as below which contains abstarct method calc -
public abstract class Shape{
public Shape(){
System.out.println("from shape");
calc();
}
public abstract void calc();
}
Another class Circle which extends Shape -
public class Circle extends Shape{
public Circle(){
System.out.println("from Circle");
}
public void calc(){
System.out.println("from calc in circle");
}
}
And now the Final main class-
public class BasicsTest{
public static void main(String [] args){
Cirlce c=new Circle();
}
}
Output when run the main class -
from shape
from calc in circle
from circle
I understand that when we create object of child class the construtor of parent class will be called. What i am confused about is how the call to calc method in shape's constrcutor works as we dont have any implementaion for the calc in shape class.
public Shape(){
System.out.println("from shape");
calc(); // how the method from child class is being called ??
}
From the output it seems it is calling the overridden calc method from the child class circle but how does that works ??
Thank you.