 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jimmy Gogo Guest
|
Posted: Tue Mar 22, 2005 12:02 pm Post subject: Subclassing and Member Access |
|
|
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
|
Posted: Tue Mar 22, 2005 12:17 pm Post subject: Re: Subclassing and Member Access |
|
|
"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 |
|
 |
|
|
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
|
|