GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods and inheritance. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods and inheritance." Watch "Static methods and inheritance." New topic
Author

Static methods and inheritance.

Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Hey guys,

Let's have a look at this piece of code:



And this code prints:
Parent get1 Child get2

The second part is clear, it's just regular overriding. But as for the part 1 "Parent get1". A static method is chosen based on the reference type during compilation time. So in the main method we are creating an instance of a Child class which has inherited get() method. Then it runs get() but still using the Child instance. So I think that the method get1() from the Child class should be chosen but it's not. The one from Parent is chosen even though we use the Child class instance.
Could someone explain me what's going on?

Cheers,
Jan.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Because we can't override static methods. It is method hidding not overriding.


Life is easy because we write the source code.....
satish agrawala
Greenhorn

Joined: Mar 04, 2010
Posts: 1
Since get1() in child class is made static,it signifies that it is not inherited but a hidden method..In case of hidden concept,the hidden methods of object's reference type is executed during runtime..

But in case of inherited method,compiler checks for availability of that method in reference superclasss(e.g-class super=new childA()) And During runtime,actual object's(childA here) method is executed.

Correct me if i m wrong..

Thanking you

Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
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.


SCJP1.4
i luv Ranch it always helps...!!!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Jan Osykowski wrote:
Could someone explain me what's going on?
Cheers,
Jan.

let's modify your code little bit.

hope it will help you.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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 ?

------------------------------------------
i modified the program



GETTING ERROR:

cannot find symbol
symbol : method get1()
location: class Parent
get1();
^
1 error


Child class extends Parent class so,all the methods of Parent would be in Child class.Then, when the call to get is made through new Child,it is not able to access,its own method ie. get1()

query 2

WHy ?

-------------------------------------------------------------------------------------------

query 3

if i comment get1() of Child and uncomment get()1 of Parent,the the code runs fine i.e






OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
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 ?


yup its true for more detail you can refer Thinking in java by Bruce Eckel 5th edition, page 244.

Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
---------------------------------------
i modified the program



GETTING ERROR:

cannot find symbol
symbol : method get1()
location: class Parent
get1();
^
1 error


Child class extends Parent class so,all the methods of Parent would be in Child class.Then, when the call to get is made through new Child,it is not able to access,its own method ie. get1()

query 2

WHy ?

Child class extends Parent class so,all the methods of Parent would be in Child class.Then, when the call to get is made through new Child,it is not able to access,its own method ie. get1()

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.

In the next modified code get1() of parent is being called. I think the two modified codes are clearing many things themselves.

And last thing although i am sure about my reply, still i would seek words from the experts.
-------------------------------------------------------------------------------------------

query 3

if i comment get1() of Child and uncomment get()1 of Parent,the the code runs fine i.e





Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

THe output of last program is

Parent get1 Child get2

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


if this is the case,then why get2() of Child class is being called and not of Parent ?

THen output should be

Parent get1 Parent get2



Ammy Singh
Ranch Hand

Joined: Oct 17, 2008
Posts: 36
if this is the case,then why get2() of Child class is being called and not of Parent ?

THen output should be

Parent get1 Parent get2



I am chnaging Child.get() to
to
Child ch = new Child();
ch.get();

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.
as in following code


or you can access it on the instance of Parent class as:
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


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.



i am confused by your two statements.
ok,agree to your second statement but your first statement seems to confuse



At one time you say that we call get() on the instance of Child class,so it chooses it own method and other time that parent class do not have any clue about the members of its subclass thus Parent class it tries to find it in itself and not in the subclass
Rahul S Mishra
Greenhorn

Joined: Sep 06, 2010
Posts: 2
Refer JavaTutorial
The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Now, in your example, in Parent.get, get1 is a class method (Remember that it is NOT overridden in Child as static methods cannot be overridden) which means that Parent.get1 can only be invoked by Parent class (or instance of Parent class - compiler will warn it but allow it, however it is not a good practice). So, it stands to reason that Parent.get1() will be invoked.

Preparing for SCJP 6, target : March 2011.
Jd Sierra
Greenhorn

Joined: Jul 09, 2011
Posts: 17

The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.



This will print child get1 child get2 as get() is in Child and it is invoked from there.
 
GeeCON Prague 2014
 
subject: Static methods and inheritance.