aspose file tools*
The moose likes Beginning Java and the fly likes Can We Access Child class members in super class in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can We Access Child class members in super class in java ?" Watch "Can We Access Child class members in super class in java ?" New topic
Author

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

kademane guru
Greenhorn

Joined: Feb 20, 2007
Posts: 21
Dear Javaranch,
In java can we access child class members in super class?
if yes give me some examples.


Regards
Kademane Gururaja
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
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;


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can We Access Child class members in super class in java ?
 
Similar Threads
protected
Accessibility of private methods in inner classes
is it create object of Base class?
Accessing static method with super
Error in K&B book