• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

private variables and inheritance.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I am having a small problem while inheriting. Let me put my problem here with an example. Lets say i have 3 classes BaseClass, ChildClass and objectClass and the following is my code.



Now lets say i am creating the object of the child class and called



Now i get null printed out. How is that?

When i am calling the method on the child class's object the method is availble via inheritence. and here when it tries to access the variable the object in the variable obj1 of the child class should be accessible since the instance is that of child class.


This is not happening. Can somebody correct me?
 
author
Posts: 23919
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

When i am calling the method on the child class's object the method is availble via inheritence. and here when it tries to access the variable the object in the variable obj1 of the child class should be accessible since the instance is that of child class.




Overriding (polymorphism) applies to methods -- not to instance variables. Since the method you are calling is a method of the base class, it uses the instance variables of the base class -- which has not been assigned in your example.

Henry
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method1() is not overridden in the ChildClass. So when you invoke method1() in ChildClass instance, actually BaseClass version is executed. In the BaseClass version obj1 is initialized as null. Hence the output is null.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inherited instance method will cannot access any thing declared in your derived class.
here method1() can access only variable declared in your superclass, so it is accessing obj1 declared in superclass and printing null.
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ December 10, 2008: Message edited by: nav katoch ]
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hahaha. The return type is missing. It should be void in this case. I think it'S a typo.
 
He does not suffer fools gladly. But this tiny ad does:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic