AAA ref1 = new CCC();
BBB ref2 = new CCC();
CCC ref3 = new CCC();
Is there anyone who can explain how it works inside the complier?
In this case ref1
is a reference to a AAA
object and since CCC
inherits from AAA
it is also an AAA
so this assignment is allowed by the compiler and by the JVM. ref2
is a reference to a BBB
and since new CCC()
is also a BBB
by inheritance this assignment is allowed.
EDIT: With your modified code, it prints as you expect as you are calling loadMethod(int)
and loadMethod(double)
In the first three calls you are using an overridden method, in the second three calls you are using an overloaded method.
Before your edit….
If you correct the code so it compiles you get
CCC's Method
CCC's Method
CCC's Method
void Method
void Method
void Method
run here http://ideone.com/r7K2wI
I sort of get what you are trying to say up to “Syso(“CCC’s Method”) must occurs as a result.” but
Each loadMethod will split out “void int and double Method” because the classes they initially used are AAA and BBB.
thanks for reading!
No, there is no way a call loadMethod()
will call loadmethod(double)
as it can’t and doesn’t know which double
you would have passed to it.
9
solved How come @override works in Java?