I love Mozart (^o^)
or polymorphism:
Mother dau= new Daughter();
dau.doStuff();
Deepak Bala wrote:Can you please edit your post and place your code under the code tags ?
I love Mozart (^o^)
Henry Wong wrote:
or polymorphism:
Mother dau= new Daughter();
dau.doStuff();
Polymorphism applies -- even during construction.
Henry
I love Mozart (^o^)
Henry Wong wrote:
or polymorphism:
Mother dau= new Daughter();
dau.doStuff();
Polymorphism applies -- even during construction.
Henry
I love Mozart (^o^)
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
I love Mozart (^o^)
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
I love Mozart (^o^)
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 :">
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
I love Mozart (^o^)
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?
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
I love Mozart (^o^)