Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Issue

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello JavaRanch Members please help me on overriding issue.

When method is overriden the method selection is done at runtime depending on actual object.

See the following code. I made a following code.




Output is as follows
Extension doIt
Extension doIt


In the above code when super class constructor is running it calls a method which is overriden. But at this step super class object creation is in progress.
As per my logic sub class object creation happens after super class object creation but here I seen that method is called on sub class object even if sub class object is not completely created.
If sub class object is not completely created how the process is going on for method call? please explain this issue.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can figure it out, the method from the sub-class itself is called and the reason behind this,I think is when the method is called from the constructor of the super class the method present lowest in the hierarchy tree is called as it always happens and in this case it is the one present in the sub-class. I am not sure about this and please correct if I am wrong.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sudipto Shekhar for your reply.
I got your point. We are on same track.

Let see what other people will think about it.
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninand,
Good question.

Sudipto,
Nice explanation.

Here is my take. doIt() means this.doIt(). So when you create an Extension and when Base's constructor is called, this.doIt() in Base refers to Extension's doIt() as 'this' refers to an Extension. If there was no doIt() in Extension, Base's doIt() would have been executed as Extension would have inherited it from Base. In Extension's constructor if you want to invoke Base's doIt() you can always call it using 'super' but even though you explicitly put a 'this' in the Base class' constructor, at runtime 'this' would refer to an Extension object and the overriden doIt() in Extension will be called.

Try putting this in the Base class' constructor, and it explains what's going on:


-Reuel.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marco Reuel Perez

You are correct. In the Basic constructor this refers to Extension object at runtime which is shown in your suggested print statement.
So now I think that there is already Extension object created when Basic constructor is running.

Let see what other people will say about my guessing
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:
In the above code when super class constructor is running it calls a method which is overriden. But at this step super class object creation is in progress.
As per my logic sub class object creation happens after super class object creation but here I seen that method is called on sub class object even if sub class object is not completely created.
If sub class object is not completely created how the process is going on for method call? please explain this issue.

Are you sure that a super class object is created?
I don't think the super call (implicit or explicit) create an object. You only create objects with the new keyword.
So in your case, there's only an Extension object in the heap.

folks ..., am I wrong
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Leandro Coutinho you are correct we can create object using new keyword but what about super class constructor or call to super() from sub class constructor and why we extend Object class implicit way?
What we say about Object class's constuctor?
 
Marco Reuel Perez
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now I think that there is already Extension object created when Basic constructor is running.


Well, created in the sense that it has been allocated space on the heap and instance variables are set to their default values and there is a reference to the object that is being created. But remember object is "created completely" only when its constructor (and it's super constructors) ran completely.

This is my understanding. Correct me if I miss something here.

-Reuel.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marco Reuel Perez I agree with you.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:
Are you sure that a super class object is created?
I don't think the super call (implicit or explicit) create an object. You only create objects with the new keyword.
So in your case, there's only an Extension object in the heap.

folks ..., am I wrong


I am afraid but when the super class constructor is called an object of the super class is also created, if not so then every thing fails! If an object of the super class is not created then why is overriding there? Whom do you override?

Think of it! Dog extends Animal-----If there is no Animal there is no Dog!! So for the Dog to be created the Animal too is to be created. And that's why the super class constructor is run before the Dog object is created, then the Dog will get the properties of Animal.

I hope this makes clear..and not boring stuff
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are perfect Sudipto Shekhar. I agree with you.
So super class object must be created before subclass object even if we created subclass object using new keyword.
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this:



"showMessage()" and "value" are private, so they cannot be inherit by Sub class in any way. When this code runs on my machine (over 100 times) I always give "super finalize 99" as output, and due to the impossibility of Sub to show this message, I think that a complete Super object is created and allocated on the heap (remember that variables aren't dinamically invoked at runtime). Also, if Super.finalize() is reached by the garbage collection mechanism, such an object must be exist on the heap!!
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all members
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic