Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods and inheritance.

 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because we can't override static methods. It is method hidding not overriding.
 
satish agrawala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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





 
Ammy Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jd Sierra
Greenhorn
Posts: 17
Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic