You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If class-A doesn't contain method m2(), then compile time error. But if class-A contains method m2(), then m2() of class-B will be called. Because variable a is referring to object of class-B,
But why:
The error occurred because of Java's static type checking,
The class of the reference variable determines which instance methods can be called on it and since class-A doesn't have method m2(), the compiler will complain,
But when we are adding m2() in class-A, compiler error will be gone. Now in runtime it will call m2() of class-B since variable a is referring to an object of class-B due to polymorphism rule.