Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange "this" behavior

 
Sun LiWei
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the output is :
method B
Super Class
in class Father,why this() calls the Father's constructor but this.method()A() calls the class Child's methodA()?
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing strange here.. polymorphic rule.. RTTI... type of the object is Child... therefore the method from child is invoked.
btw... preferably don't use this style of coding unless the invoked methods are private or final. It could cost a hapless maintenance chap a lot of sleepless nights.
 
pree sree
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects always know 'who they are ' on the basis of instanceof operator.
When an object is created the instances of all its super classes are also created.at runtime it is decided which implementation is called.(except for static methods which are resolved at compile time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic