Subclassing and Member Access
04/12/2018 12:08

I have extended a class with a subclass in a different package.
I want my subclass to have access to member variables in super classwithout having to make those public. How do I do this?
Thanks, Jimmy
Source is Usenet: comp.lang.java.help
Sign in to add a comment
Answer score: 5

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
Source is Usenet: comp.lang.java.help
Sign in to add a comment