public class A {
public void m1() {
System.out.println("A m1");
}
}
public class B extends A {
public void m1() {
System.out.println("B m1");
}
public void m2() {
System.out.println("B m2");
}
}
public class Result {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*A a = new A();
a.m1();
B b = new B();
b.m1();
b.m2();
*/
A ab = new B();
ab.m1();
}
}
In the above code, I have a Class A and Class B that extends Class A. In class C I am creating the object of class B and it's been assigned to class A. when I try to call ab.m1() it calling the method in class B.but when I try to call ab.m2() i get compile time error. I am not understanding why class B method is called during ab.m1(). can someone help me in understanding the concept much better.thanks in advance.