• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Polymorphism and Constructors

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output is


Why is the output not


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Base class won't know what all are there in its extended classes.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
((Base) b).amethod();
should give the output
Derived.amethod()-1

It worked.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In what order does the initialization takes place?
At what point the subclass gets initialized?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic