• 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

Can We Access Child class members in super class in java ?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Javaranch,
In java can we access child class members in super class?
if yes give me some examples.


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

kademane guru wrote:Dear Javaranch,
In java can we access child class members in super class?
if yes give me some examples.



No we can't access any members of child class in parent class;
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can, but it is not what you want. When your program is designed well, superclasses should not know anything about the internals of their subclasses.

Here is an ugly example, in which I cast this to the subclass type (I recommend that you never do this in a real application, this is just to show that it's possible!):

Sometimes, however, you might want to create an abstract superclass that leaves some parts to be implemented by subclasses. This version is much better than the example above, because the superclass doesn't need to cast this to a specific subclass type and doesn't need to access the internals of the subclass:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic