Scenario
public class Parent {
public void m1(double d) {
System.out.println("double : "+ d);
}
}
class Child extends Parent {
public void m1(int d) {
System.out.println("int : "+ d);
}
}
class Demo {
public static void main(String[] args) {
Parent p = new Child();
p.m1(5);
}
}
Which method would be called - parent or child one ?
Explanation
Parent class method will be called by passing int value into double type parameter because method are overloaded (Not overridden)
Overloading is compile time binding, where compiler already decided to call method of parent as it will pick the declared reference type (Not object type)
Result
double : 5.0
No comments:
Post a Comment
Note: only a member of this blog may post a comment.