Parent get1 Child get2
Life is easy because we write the source code.....
SCJP1.4
i luv Ranch it always helps...!!!
Jan Osykowski wrote:
Could someone explain me what's going on?
Cheers,
Jan.
BY AMAN
Subject: Static methods and inheritance.
You are not overriding get(), you are actually accessing a hidden method ( get() ) of your base class ,When you create an object of your child class it gets a copy of its parent class object ( which is created by the compiler implicitly, and is called sub-object ) which enable it (i.e subclass object) to access hidden members of its parent class.
Experts please point out if i am wrong.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:
BY AMAN
Subject: Static methods and inheritance.
You are not overriding get(), you are actually accessing a hidden method ( get() ) of your base class ,When you create an object of your child class it gets a copy of its parent class object ( which is created by the compiler implicitly, and is called sub-object ) which enable it (i.e subclass object) to access hidden members of its parent class.
Experts please point out if i am wrong.
query 1
Is it true ?
SCJP1.4
i luv Ranch it always helps...!!!
SCJP1.4
i luv Ranch it always helps...!!!
Because a parent class do not have any clue about the members of its subclass. (Inheritance follow forward hierarchy and not backward) , so when you try to access get1() in parent class it tries to find it in itself and not in the subclass, since its not in there so it prompt an error
OCPJP 6.0 93%
OCPJWCD 5.0 98%
Child ch = new Child();
ch.get();
SCJP1.4
i luv Ranch it always helps...!!!
Because a parent class do not have any clue about the members of its subclass. (Inheritance follow forward hierarchy and not backward) , so when you try to access get1() in parent class it tries to find it in itself and not in the subclass, since its not in there so it prompt an error
Here we are calling get() on the instance of Child class which has its own get2(), so it prefers its own method and override it and not the one in parent class. If you remove the get2() in Child class then it will access the get2() of Parent.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Preparing for SCJP 6, target : March 2011.
Consider Paul's rocket mass heater. |