• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism and Constructors  RSS feed

 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output is


Why is the output not


 
Ganesh Ramachandran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Base class won't know what all are there in its extended classes.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, method polymorphism always applies, even before the object has finished initialization. You can see this, by observing that the subclass method is called from the base class constructor.

Second, the initialization order (for the constructor) for the class is, the super(...) is called, the instance variables (and instance initializers) are called (in source code order), and finally, the rest of the constructor is called. So, when the superclass constructor is running, the instance variables still have their default values -- which in the case of int, is zero.

Henry

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,you had already seen the effect of calling the overridable method in a constructor.that's why it's sometime recommended to avoid such calls.
Anyway,what do you think is the reason behind the first line in the output-i mean why it is not Base?

Praveen
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
Anyway,what do you think is the reason behind the first line in the output-i mean why it is not Base?

Praveen


Hi Praveen.
I thought, it was because of method overriding. Is that wrong?

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham wrote:Hi Praveen.
I thought, it was because of method overriding. Is that wrong?
No,it's correct.have a look at one more thing:try to think about output of this statement after line 15 in main()((Base) b).amethod();

Praveen
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
((Base) b).amethod();
should give the output
Derived.amethod()-1

It worked.
 
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
Gautham Muralidharan wrote:
It worked.


Yes, this works.... but ... this always worked, even without the cast.

The zero didn't come from the main() method call, it came from the call in the base class, when the subclass hasn't finish initializing yet.

Henry
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what order does the initialization takes place?
At what point the subclass gets initialized?
 
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
Gautham Muralidharan wrote:In what order does the initialization takes place?
At what point the subclass gets initialized?


As already mentioned in my previous post...

Henry Wong wrote:
Second, the initialization order (for the constructor) for the class is, the super(...) is called, the instance variables (and instance initializers) are called (in source code order), and finally, the rest of the constructor is called. So, when the superclass constructor is running, the instance variables still have their default values -- which in the case of int, is zero.


Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham Muralidharan wrote:((Base) b).amethod();
should give the output
Derived.amethod()-1

It worked.

Yes you are correct,it would work.
see this code:

It would produce the output:

you can see the different behavior of the cast for the method invocation and the field.Actually when we perform cast on the object to some type then it's some time mis-interpreted as the class of an object is changed to the cast type but it's not like that.cast doesn't change the class of an object(in fact not possible),what it does is that it checks for if the type of the object is compatible with the cast type.now since the instance method is polymorphic we get such results(of the code above) but in case of fields(they are not polymorphic),cast perform a role of assuming(yes just assuming) if the class of object is the cast type and thus printing the corresponding value(Accordingly with the cast type).
if the variable has a package access then you can note that the casting and accessing the field with super keyword will give you the same results.
also several of the time i have seen many people saying that *super* is a reference to the parent object.it is almost incorrect.*super* as well as *this* both are reference to the current object.the difference is how they were interpreted super lets the Jvm to think like if the class of a current object is the super class(though the class remains the same whatever it is),here the super has different behavior than the cast(as we had in case of variable).though the method is polymorphic the super let's us to call the method of super class.
May be these things will help you in learning(easy).

KindRegards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham wrote:In what order does the initialization takes place?
At what point the subclass gets initialized?

Along with the Henry's answer you can go through this link→Java® Language Specification(12.4).

Kind Regards,
Praveen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!