AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Subclassing and Member Access

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Jimmy Gogo
Guest





PostPosted: Tue Mar 22, 2005 12:02 pm    Post subject: Subclassing and Member Access Reply with quote



Hi all.

I have extended a class with a subclass in a different package.
I want my subclass to have access to member variables in super class
without having to make those public. How do I do this?

Thanks, Jimmy
Back to top
Bjorn Abelli
Guest





PostPosted: Tue Mar 22, 2005 12:17 pm    Post subject: Re: Subclassing and Member Access Reply with quote




"Jimmy Gogo" wrote...

Quote:
I have extended a class with a subclass in a different package.
I want my subclass to have access to member variables in super class
without having to make those public. How do I do this?

Java has four types of member visibility:

- private:
members are only seen from the actual class
and instances of that class

- public:
members are seen from all classes
and instances of all classes, for which
the actual class is reachable.

- package (default):
members are only seen from the actual class
or classes in the same package
and instances of that class
or classes in the same package

- protected: (this is the one you want)
members are only seen from the actual class
or subclasses of it
or classes in the same package
and instances of that class
or subclasses of it
or classes in the same package

....so...

package superPackage;

public class MySuper
{
private int a;
public int b;
int c; // default package-visibility
protected int d;
}

==================================

import superPackage.*;

public class MySub extends MySuper
{
// From here you can only reach "b" and "d"
// but as you didn't want "public" visibility
// you need to declare them "protected" in
// your superclass
}


// Bjorn A



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.