Member Accessibility in Package

Public Members
public member is accessible from anywhere, within a same package or other package. They are inherited in same subclass or other classes.
package packageA; public class ParentClassA { public int parent_a; public void parentMethodA() { System.out.println("Parent class method A"); } } class ChildClassA extends ParentClassA { void childMethodA() { parent_a=23; } }

Sava as : ParentClassA.java

package myPackB; import packageA.*; public class ParentClassB extends ParentClassA { public int parent_b; ParentClassA objA=new ParentClassA(); ParentClassB objB=new ParentClassB(); public void parentMethodB() { parentMethodA(); objA.parentMethodA(); objA.parent_a=77; objB.parentMethodA(); objB.parent_a=90; System.out.println("Parent class method A"); } }

save as : parentClassB.java

Output

C:\JavaProgram\package\Accessibility>javac -d . ParentClassA.java

C:\JavaProgram\package\Accessibility>javac -d . ParentClassB.java

Download this example

Protected Members
A protected member accessible in all classes in the same package, and by all subclasses of its class in any other packages where this is code is accessible.
A subclass in another package can only access protected members in super class through the object of subclass.
package myPackA; public class ParentClassA { protected int parent_a; protected void parentMethodA() { System.out.println("Parent class method A"); } } class ChildClassA extends ParentClassA { void childMethodA() { parent_a=23; } }

save as : ParentClassA.java

package myPackB; import myPackA.*; public class ParentClassB extends ParentClassA { ParentClassA objA=new ParentClassA(); ParentClassB objB=new ParentClassB(); public int parent_b; public void parentMethodB() { objB.parentMethodA(); objB.parent_a=77; objA.parentMethodA(); objA.parent_a=90; System.out.println("Parent class method A"); } }

save as : ParentClassB.java

Output

C:\JavaProgram\package\ProtectedAccessibility>javac -d . ParentClassA.java

C:\JavaProgram\package\ProtectedAccessibility>javac -d . ParentClassB.java
ParentClassB.java:16: parentMethodA() has protected access in myPackA.ParentClassA
objA.parentMethodA();
^
ParentClassB.java:17: parent_a has protected access in myPackA.ParentClassA
objA.parent_a=90;
^
2 errors

Download this example

Default Members
If no accessibility modifiers is specified the member is only accessible through other classes in its same class’ s package. Even if its class is extends in another package, the member is not accessible elsewhere.
package myPackA; public class ParentClassA { int parent_a; void parentMethodA() { System.out.println("Parent class method A"); } } class ChildClassA extends ParentClassA { void childMethodA() { parent_a=23; } }

save as : ParentClassA.java

package myPackB; import myPackA.*; public class ParentClassB extends ParentClassA { ParentClassA objA=new ParentClassA(); ParentClassB objB=new ParentClassB(); public int parent_b; public void parentMethodB() { objB.parentMethodA(); objB.parent_a=77; objA.parentMethodA(); objA.parent_a=90; System.out.println("Parent class method A"); } }

save as : ParentClassB.java

Output


C:\JavaProgram\package\DefaultAccessibility>javac -d . ParentClassA.java

C:\JavaProgram\package\DefaultAccessibility>javac -d . ParentClassB.java
ParentClassB.java:14: cannot find symbol
symbol : method parentMethodA()
location: class myPackB.ParentClassB
objB.parentMethodA();
^
ParentClassB.java:15: parent_a is not public in myPackA.ParentClassA; cannot be
accessed from outside package
objB.parent_a=77;
^
ParentClassB.java:16: parentMethodA() is not public in myPackA.ParentClassA; can not be accessed from outside package
objA.parentMethodA();
^
ParentClassB.java:17: parent_a is not public in myPackA.ParentClassA; cannot be accessed from outside package
objA.parent_a=90;
^
4 errors

Download this example

Private Members
package myPackA; public class ParentClassA { private int parent_a; private void parentMethodA() { System.out.println("Parent class method A"); } } class ChildClassA extends ParentClassA { void childMethodA() { parentMethodA(); parent_a=23; } }

save as : ParentClassA.java

Output


C:\JavaProgram\package\PrivateAccessibility>javac -d . ParentClassA.java
ParentClassA.java:17: cannot find symbol
symbol : method parentMethodA()
location: class myPackA.ChildClassA
parentMethodA();
^
ParentClassA.java:18: parent_a has private access in myPackA.ParentClassA
parent_a=23;
^
2 errors

Download this example

 



 

Latest Tutorial


Most Visited Tutorial