问题
i have just started learning java::Inheritance and confused while mixing Up-Casting.
class Example{
public void methodOne(){
System.out.println("Example::Method_1");
}
public void methodTwo(){
System.out.println("Example::Method_2");
}
}
public class Test extends Example{
public void methodTwo(){ //Method overriding
System.out.println("Test::Method_2");
}
public void methodThree(){
System.out.println("Test::Method_3");
}
public static void main(String[] args){
Example exa = new Test(); // UpCasting
exa.methodOne(); // Printing Example::Method_1
exa.methodTwo(); // Printing Test::Method_2
// exa.methodThree(); // Error : can not find symbol
}
}
may someone please explain, what happening here??
回答1:
When using inheritance, the compile-time type of the reference to an object on which you call a method is only used to see (at compile time) if the method may be invoked.
But at the invocation time, it does not matter what that compile-time type is. What really matters in this case is the runtime type of the object. It is Test
, so the method is searched on Test
first.
For methodOne()
it is a bit different: it is not overriden by Test
, so the version from its superclass (Example
) is invoked.
回答2:
Class Test
extends Example
and overrides the implementation of the methodTwo
while adding new method methodThree
. Now in you main
public static void main(String[] args){
Example exa = new Test(); // UpCasting
exa.methodOne(); // Printing Example::Method_1
exa.methodTwo(); // Printing Test::Method_2
// exa.methodThree(); // Error : can not find symbol
}
Static type of the variable exa
is Example
hence API usage bounded by public methods defined in Example
class therefore it's illegal to call exa.methodThree()
(not defined it Example
).
While for methods which are define at Example
class there is a dynamic binding at runtime, which leads to concrete implementation based on the reference pointed by exa
variable. Therefore exa.methodTwo()
call leads to execution of overrided function, since dynamic type is Test
. And since Test
doesn't override methodOne
calling exa.methodOne()
will lead to the implementation defined by Example
.
回答3:
if we create object of subclass and use reference of supper class then runtime time dynamic binding happens means at runtime java decides that which thing should get called.
in your case :
1. exa.methodOne();
methodOne belongs to super class and its not being overridden in child class so parent class method will be executed.
here if you create object like Test test = new Test();
then also test.methodOne()
will give you the same result.
exa.methodTwo();
here methodTwo is being overridden in child class so at run time overridden method will get bonded and same will get executed.in third case its giving error because you are using parent class reference but methodThree() is not part of parent so compile error is being thrown. if wanna call third method then you can call like this :
Test test = new Test();
test.methodThree()
FYI,
for safety we should always use @Override annotation in child call methods if we want to override any parent class method. it will help to avoid any method override glitches.
来源:https://stackoverflow.com/questions/45660541/why-overridden-method-calling-from-subclass-if-i-have-done-up-casting