Subclassing and Member Access
04/12/2018 12:08
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 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
04/12/2018 12:08 - Jimmy Gogo wrote...


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

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below