• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about inheritance

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know if this question or doubt I am having is already answered before in this forum, so please excuse me if it has been or if it is a sily question. But I could not understand the behaviour of the following code snippets.



And the output was

10Test
20TestChild1

I could not understand why test.a and test.b would print the superclass value but when we invoke the method it is the other way round. Really confused.

 
Marshal
Posts: 79240
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods might be polymorphic, but fields aren't. Start with this FAQ, and if that doesn't help, ask again.
 
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaustav,
Casting an object to a parent class is called upcasting and is done automatically. If you upcast an object, it will lose all it's properties, which were inherited from below it's current position. Hence test.a and test.b prints out the parent data from its parent class. Note, that data will not be lost, you just can't use it, until you downcast to the right level. Why this behavior? Because in inheritance parent class knows nothing about its child class properties, but the child class knows about its parent data as it inherited it.

To overcome this, what you need to do is use polymormism. Polymorphism uses automatic downcast during method calls. Which is what happened in the getValues method (overriden method). As test contains a reference to child object it calls child's getValues (after downcasting) where trivially, the child data is printed.
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood that for static methods instance does no make any difference. But what for instance variable ? Instance variables are specific to a particular instance, why are they as per the declaration ?
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't it say anything about fields in the FAQ?
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it didn't say anything about fields. Fields are hidden, rather like static members.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic