• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help me please! Constructor Calls Overridable Method

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Mother

{

public Mother()

{

doStuff();

}

protected void doStuff()

{

System.out.println("Mother's doStuff");

}

}

public class Daughter extends Mother

{

public Daughter()

{



}

protected void doStuff()

{

System.out.println("Daughter's doStuff");

}

public static void main(String[] args)

{

Daughter dau= new Daughter();

}

}

Run the above code, the result is :

Daughter's doStuff

Please explain for me why JVM chooses Daughter's but Mother's. I thought overridden version of Daughter is called in case

Daughter dau= new Daughter();

dau.doStuff();

or polymorphism:

Mother dau= new Daughter();

dau.doStuff();
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please edit your post and place your code under the code tags ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


or polymorphism:

Mother dau= new Daughter();

dau.doStuff();



Polymorphism applies -- even during construction.

Henry
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:Can you please edit your post and place your code under the code tags ?



Run the above code, the result is :

Please explain for me why JVM chooses Daughter's but Mother's. I thought overridden version of Daughter is called in case


or polymorphism:
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:


or polymorphism:

Mother dau= new Daughter();

dau.doStuff();



Polymorphism applies -- even during construction.

Henry


thanks for your help. I have guessed that but I haven't read any document saying about this
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:


or polymorphism:

Mother dau= new Daughter();

dau.doStuff();



Polymorphism applies -- even during construction.

Henry


Thanks for your help! I've guessed like it! But I haven't found any article saying this
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you are in an instance context in your code (an instance method, a constructor, an instance initialization block, or an instance variable initializer,) any unqualified reference is as if it were performed through the this reference. You just need to know what this means. It is a reference variable whose type is the class it is coded in, and that refers to an object which is:
- If in an instance method, the object used to call the instance method.
- If in an instance initialization block, the object being constructed.
- If in an instance variable initializer, the object being constructed.
- If in a constructor, the object being constructed.

Once you know what this is, then you just apply the usual rules. In the case that you show, since doStuff() is an instance method in the hierarchy, you need to consider overriding when resolving the method which is actually called. What that means is that you need to call the method defined in the class of the actual object which this points to. In this case, that class is Daughter, as per the discussion above.

This all might sound too technical, but it's the way it actually works.
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Whenever you are in an instance context in your code (an instance method, a constructor, an instance initialization block, or an instance variable initializer,) any unqualified reference is as if it were performed through the this reference. You just need to know what this means. It is a reference variable whose type is the class it is coded in, and that refers to an object which is:
- If in an instance method, the object used to call the instance method.
- If in an instance initialization block, the object being constructed.
- If in an instance variable initializer, the object being constructed.
- If in a constructor, the object being constructed.

Once you know what this is, then you just apply the usual rules. In the case that you show, since doStuff() is an instance method in the hierarchy, you need to consider overriding when resolving the method which is actually called. What that means is that you need to call the method defined in the class of the actual object which this points to. In this case, that class is Daughter, as per the discussion above.

This all might sound too technical, but it's the way it actually works.



thank you so much, I completely understand!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome, I'm glad that helped.

Also, I'd like to add that if you have time and patience, The Java Language Specification (3rd Edition), which is the official Java specification from Sun, and which is freely available from Sun, is the book which contains every answer to any question (at least those questions which deal with the language specification.) There are very detailed sections on method resolution and the meaning of the this reference, but basically what I told you is the condensed version.
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:You are welcome, I'm glad that helped.

Also, I'd like to add that if you have time and patience, The Java Language Specification (3rd Edition), which is the official Java specification from Sun, and which is freely available from Sun, is the book which contains every answer to any question (at least those questions which deal with the language specification.) There are very detailed sections on method resolution and the meaning of the this reference, but basically what I told you is the condensed version.


thanks again! You're so kind! I downloaded it and now I'm reading it. It seems to do me good! Because I always want to know the root of problem, only when I know it, I can remember :">
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:
thanks again! You're so kind! I downloaded it and now I'm reading it. It seems to do me good! Because I always want to know the root of problem, only when I know it, I can remember :">


The JLS is a wonderful resource, but be careful that you do not get into it deeper than you need for the SCJP. I think that for your long term understanding it is an invaluable source of information, but there are many things that are overly technical and that you don't need to understand for the exam. Some of the parts that I do really recommend (and which are rarely treated strictly enough in any texts) are those related to type conversion.
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:

yen hoang wrote:
thanks again! You're so kind! I downloaded it and now I'm reading it. It seems to do me good! Because I always want to know the root of problem, only when I know it, I can remember :">


The JLS is a wonderful resource, but be careful that you do not get into it deeper than you need for the SCJP. I think that for your long term understanding it is an invaluable source of information, but there are many things that are overly technical and that you don't need to understand for the exam. Some of the parts that I do really recommend (and which are rarely treated strictly enough in any texts) are those related to type conversion.


Thanks for your advice! Type conversion is an important part. But the part makes me be so worried is threads. I can't predict exactly what code will do! Can you give you some advices?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:
Thanks for your advice! Type conversion is an important part. But the part makes me be so worried is threads. I can't predict exactly what code will do! Can you give you some advices?


I think the best advice I can give you is to practice and ask questions here when you don't understand something. There are also a lot of questions on threads in the forum that you can look up. Sorry if this is not the answer you expected, but there are no shorcuts.
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:

yen hoang wrote:
Thanks for your advice! Type conversion is an important part. But the part makes me be so worried is threads. I can't predict exactly what code will do! Can you give you some advices?


I think the best advice I can give you is to practice and ask questions here when you don't understand something. There are also a lot of questions on threads in the forum that you can look up. Sorry if this is not the answer you expected, but there are no shorcuts.


It's ok. I know I am trying...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a side note: Note that inheritance and subclasses in Java (and most other object oriented programming languages) is not really analogous to biological inheritance. So naming classes "Mother" and "Daughter" (or "Father" and "Son" as is also sometimes done) is misleading.

The concept is the following: A subclass is a specialized version of its superclass. It's important to understand this is-a relationship. In the case of Mother and Daugther, in the biological sense it is not true that a Daughter is a Mother - but this is what your class model means.

A better example would be for example to call your superclass Mammal, and your subclass Dog - because a Dog is a Mammal.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So after reading this post and another one about static inheritance...I wanted to see the differences between instance inheritance and static inheritance.

I modified the example above to include a static method that is inherited by Daughter. Notice that Mother.foobar() is called in its constructor.

 
reply
    Bookmark Topic Watch Topic
  • New Topic