• Post Reply Bookmark Topic Watch Topic
  • New Topic

help sought in constructor chaining ?  RSS feed

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose we have a class hierarchy for example



When we create object o labrador , does objects of all the supertypes of labrador all the way up to Objects also gets created, since their constructors are run when we invoke labrador constructor ??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly one object is created. That one object IS-A Labrador and IS-A Dog and IS-AN Animal. Each of those classes' constructors are invoked, but it's just one object.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply Jeff. But i have one more query regarding that. consider following class hierarchy






When i create object of labrador class like this labrador l = new labrador() and call labrador.dogTest() , "dog test overridden" will be printed and then super.dogTest() will be called. my question is that you said that only one object will be created , here in our case that object will be labrador , which as you said will be a dog, an animal and an Object. so when we did super.dogTest() , it calls dogTest method of dog class, which prints "dog method". if there is just one object , that is of labrador, how come we call dogTest method of dog class, since there are no dog objects.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:since there are no dog objects.


But that's just wrong. We have already discussed that -- there is one object and it is a Dog. It's also an Animal and an Object and a labrador.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a Dog object. There just isn't a separate Dog object. There's one object that is-a Labrador and is-a Dog and is-an Animal. Everything you can do to a Dog, you can do to a Labrador.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i got it that there is just one object which is labrador and which IS-A DOG, IS-A ANIMAL and IS-A OBJECT. that means when i did super.dogTest() the method gets called on labrador object which IS-A dog. but labrador has overridden dogTest object in it. right. that means labrador object doesn'nt have dog version of dogTest in it. does that mean labrador object has both versions of dogTest viz. its own overridden and one inherited from Dog. i have posted a question earlier which goes like this :



i asked how many methods will dog class has . and i got the answer 2, whereas i thought answer is 3. according to me dog object has 3 methods viz. its inherited eat method from animal class, overridden eat method and run method.

so coming to the main problem if there is one labrador object , and super.dogTest() succeeds , that means labrador has both versions of dogTest, which means in my earlier post answer should be 3 and not 2. kindly explain , i think i'm getting confused.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to the above posts:- A Labrador object is a Dog object. The Labrador class inherits all the members of the Dog class. That means that all members from Dog are copied over to Labrador. In this case, Labrador overrides the dogTest method. You can think of overriding like this. Labrador gets a copy of Dog's dogTest method, but since Labrador implements it's own dogTest method, Labrador's dogTest hides Dog's dogTest. So, the Dog's dogTest still exists inside Labrador but is hidden from everyone. Labrador can call the hidden method using super.dogTest
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:but labrador has overridden dogTest object in it. right. that means labrador object doesn'nt have dog version of dogTest in it.


That is your assumption, but you can see from this simple test that this assumption is incorrect.

does that mean labrador object has both versions of dogTest viz. its own overridden and one inherited from Dog.


Yes.

i have posted a question earlier which goes like this :



i asked how many methods will dog class has . and i got the answer 2, whereas i thought answer is 3. according to me dog object has 3 methods viz. its inherited eat method from animal class, overridden eat method and run method.


That depends on how you count methods. There is not standard meaning for "Class X has N methods." You need to qualify what you mean. If the question just says, "How many methods does the dog class have?" then it is either a horrible question because it's not clear, or else it's a good way to get a look into your thought process as you discuss what could be meant by it and look for clarification.

If you're going to count the overridden eat() method, though, you should also count Objects equals(), toString(), hashCode(), wait(), notify(), notifyAll() as well. And what about the protected method clone()?

so coming to the main problem if there is one labrador object , and super.dogTest() succeeds , that means labrador has both versions of dogTest, which means in my earlier post answer should be 3 and not 2. kindly explain , i think i'm getting confused.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote: That means that all members from Dog are copied over to Labrador.


No, I wouldn't say that. Just say they're present. Saying they're "copied" implies and implementation detail that isn't specified.

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys. just to summarize. when we override a method , the overriden method is present in the overriding class along with the overridding method . it is just hidden. the class has both vesions of the method. please confirm it.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:thanks guys. just to summarize. when we override a method , the overriden method is present in the overriding class along with the overridding method . it is just hidden. the class has both vesions of the method. please confirm it.



Yeah, except that "hidden" has a specific meaning in the JLS. In particular, for example, a static method that has the same signature in the parent class and subclass is not overridden, it is hidden.

So instead of saying the overridden method is "hidden" (since a method can be either overridden or hidden, but not both) I would say the overridden method is not accessible except by using super from within the child class itself.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:thanks guys. just to summarize. when we override a method , the overriden method is present in the overriding class along with the overridding method . it is just hidden. the class has both vesions of the method. please confirm it.




No. When you override a class, the subclass do *not* have both implementations of the method. It is not hidden -- it just doesn't exist. If the JVM needs to get to the overridden method of the superclass, it just uses the definition in the superclass.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
gurpeet singh wrote:thanks guys. just to summarize. when we override a method , the overriden method is present in the overriding class along with the overridding method . it is just hidden. the class has both vesions of the method. please confirm it.




No. When you override a class, the subclass do *not* have both implementations of the method. It is not hidden -- it just doesn't exist. If the JVM needs to get to the overridden method of the superclass, it just uses the definition in the superclass.

Henry


@gurpeet: Henry is correct. When I said that you were correct in saying that the overridden method is present in the subclass, I meant it in the sense that it's accessible to the single object that IS-A Sub and IS-A Super. The method definition--the executable code making up the body--is definitely NOT copied into the subclass. It is merely accessible through an object of the subclass via the parent class definition.

So while there is only a single object that is both Sub and Super, the class definitions are still separate.

I apologize for any confusion my imprecision may have caused.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as i was on the verge of understanding the problem , the recent answers have made me confused. so let me put this way guys. earlier i arrived at the conclusion that sub class will have both versions of the methods. one from the parent class and one which it has overridden. in short both versions will be present but super class one will not be accessible(rather than saying hidden). but putting into account recent posts from Henry, i conclude that , the sub class doesnot have both versions of the method. it has just one overidden version present and it can call the superclass version using super keyword. putting in another way , it is the superclass method that gets called using super, but through subclass object. guys please confirm it so that there is no more confusion .
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:as i was on the verge of understanding the problem , the recent answers have made me confused. so let me put this way guys. earlier i arrived at the conclusion that sub class will have both versions of the methods. one from the parent class and one which it has overridden. in short both versions will be present but super class one will not be accessible(rather than saying hidden). but putting into account recent posts from Henry, i conclude that , the sub class doesnot have both versions of the method. it has just one overidden version present and it can call the superclass version using super keyword. putting in another way , it is the superclass method that gets called using super, but through subclass object. guys please confirm it so that there is no more confusion .
yes
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:the sub class doesnot have both versions of the method. it has just one overidden version present


Correct.

and it can call the superclass version using super keyword.


Correct.

putting in another way , it is the superclass method that gets called using super,


Correct.

but through subclass object.


That depends what you mean. If you mean "it can only be called through an instance of a subclass, not by any old arbitrary class," then, yes, you're correct.

On the other hand, if you mean something like "It's called through the subclass object, not through the superclass object," then remember, there is only one object. There are not separate "subclass object" vs. "superclass object."

There is one object, but there are multiple class definitions that together specify what data that object contains and what operations it can perform.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!