Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Understanding behaviour of this keyword

 
Greenhorn
Posts: 10
2
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one doubt on below code snippet. As per my understanding 'this' keyword means current object. When a super constructor is invoked and we print hashcode on this code, why we don't get a hashcode for parent object. On execution I see same hashcode is printed twice. Can anyone explain this behavior to me.

 
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you don't have separate objects for the supertype and the subtype. When you create an instance of a subtype, that object is not only of the subtype, but also of all supertypes.
 
Marshal
Posts: 76491
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you only have one object. You are not creating a superclass object separate from the subclass object.

By the way: parent and child are inaccurate terms for superclass and subclass; the refer to biological inheritance which is different from object inheritance. Maybe the terms they use in C# are the best: base and derived classes.
 
Stephan van Hulst
Saloon Keeper
Posts: 14327
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say you have a type Programmer and it's a subtype of Person.

We create a Programmer with name "Bhupinder Verma". Regardless of whether we refer to the object as a Programmer or a Person, it's still the same single person, named "Bhupinder Verma".
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic