aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Issue" Watch "Overriding Issue" New topic
Author

Overriding Issue

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

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.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

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.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

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

Joined: Apr 16, 2009
Posts: 45
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.


SCJP 6 - 91%
<><
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

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

Joined: Mar 04, 2009
Posts: 417
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

Joined: Aug 31, 2007
Posts: 791

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

Joined: Apr 16, 2009
Posts: 45
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

Joined: Aug 31, 2007
Posts: 791

Hello Marco Reuel Perez I agree with you.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

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

Joined: Aug 31, 2007
Posts: 791

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

Joined: Feb 23, 2009
Posts: 44
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!!


SCJP 6
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

Thanks to all members
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding Issue