SCJP1.4
SCJP1.4(91%)
"The compiler is able to determine which method (amethod) is called, depending on the object on which the method is called. If the object is a Child object, then the output will be "Derived.amethod()" and if the object is a Parent then the output will be "Base.amethod()"
When you're creating object like
Parent p = new Child();
and if : class Parent and class Child has variable i, and method m(), then
Sample rule:
* p.i - is a value of Parent's i (always)
* p.m() - is a method of Child (if it's overridden,if they're static, then mothod of Parent will be called)
SCJP1.4
J.L.S. 8.4.6.2 Hiding by Class (Static) Methods
"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclass ... otherwise accessible ..."
J.L.S. 8.4.6.2 Hiding by Class (Static) Methods
"A hidden method can be accessed by using a qualified name or by using a method invocation expression(15.11) that contains the keyword super or a cast to a superclass type."
Originally posted by Jamal Hasanov:
Hi, Kelvin
When you're creating object like
Parent p = new Child();
and if : class Parent and class Child has variable i, and method m(), then
Sample rule:
p.i - is a value of Parent's i (always)
p.m() - is a method of Child (if it's overridden,if they're static, then mothod of Parent will be called)
That's all.
P.S. I advice to change members and methods(i and amethod() ) to static and non-static,compile and test it. - The best way to learn+remember is practice.
Regards,
Jamal Hasanov
www.j-think.com
SCJP2. Please Indent your code using UBB Code
-When a non static method is invoked on an object using a refrence, it is the class of the current object denoted by the refrence,that determines which method to be invoked.
-When a variable or static method is invoked on an object using a refrence, it is the type of refrence not the class of the current object,that determines which method to be invoked.
Regards, Manish
SCJP 1.4
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Don't get me started about those stupid light bulbs. |