0% found this document useful (0 votes)
306 views4 pages

8 A Access Protection in Java Packages

The document discusses access protection in Java packages. It explains that access modifiers like private, protected, public and default control the accessibility of classes and members across packages. Private members can only be accessed within their own class, while protected members can be accessed by child classes in the same or other packages. Public members can be accessed anywhere, and default members can be accessed within the same package only. An example code demonstrates how different access modifiers work for a parent and child class in the same package.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
306 views4 pages

8 A Access Protection in Java Packages

The document discusses access protection in Java packages. It explains that access modifiers like private, protected, public and default control the accessibility of classes and members across packages. Private members can only be accessed within their own class, while protected members can be accessed by child classes in the same or other packages. Public members can be accessed anywhere, and default members can be accessed within the same package only. An example code demonstrates how different access modifiers work for a parent and child class in the same package.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9/1/2021 Java Tutorials - Access protection in java packages

(../index.html)

  (../index.html)   (../courses.html) ✏  (../authors.html)


  (../downloads.html)   (../contact-us.html) 
Java
Programming
Access protection in java packages
 Previous (java-defining-packages.html)
Next  (java-importing-packages.html)

In java, the access modifiers define the accessibility of the class and its members. For example,
private members are accessible within the same class members only. Java has four access

modifiers, and they are default, private, protected, and public.

In java, the package is a container of classes, sub-classes, interfaces, and sub-packages. The
class acts as a container of data and methods. So, the access modifier decides the accessibility of

class members across the different packages.

In java, the accessibility of the members of a class or interface depends on its access specifiers.

The following table provides information about the visibility of both data members and methods.

www.btechsmartclass.com/java/java-access-protection-in-packages.html 1/4
9/1/2021 Java Tutorials - Access protection in java packages

🔔 The public members can be accessed everywhere.

🔔 The private members can be accessed only inside the same class.

🔔 The protected members are accessible to every child class (same package or other

packages).

🔔 The default members are accessible within the same package but not outside the

package.

Let's look at the following example java code.

www.btechsmartclass.com/java/java-access-protection-in-packages.html 2/4
9/1/2021 Java Tutorials - Access protection in java packages

class ParentClass{

int a = 10;

public int b = 20;

protected int c = 30;

private int d = 40;

void showData() {

System.out.println("Inside ParentClass");

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

System.out.println("d = " + d);


}

class ChildClass extends ParentClass{

void accessData() {

System.out.println("Inside ChildClass");

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

//System.out.println("d = " + d); // private member can't be accessed

public class AccessModifiersExample {

public static void main(String[] args) {

ChildClass obj = new ChildClass();

obj.showData();

obj.accessData();

When we run this code, it produce the following output.

www.btechsmartclass.com/java/java-access-protection-in-packages.html 3/4
9/1/2021 Java Tutorials - Access protection in java packages

 Previous (java-defining-packages.html)
Next  (java-importing-packages.html)

Courses (../courses.html) | Downloads (../downloads.html) | About Us (../authors.html) | Contcat Us


(../contact-us.html)

Website designed by Rajinikanth B


(https://www.facebook.com/btechsmartclass/)  
(https://plus.google.com/116706303219518006305)   (https://twitter.com/BtechClass)  
(https://www.youtube.com/channel/UC9YHZpCPtRqbYpu-NVP8Ecg)

www.btechsmartclass.com/java/java-access-protection-in-packages.html 4/4

You might also like