0% found this document useful (0 votes)
140 views

Topics: Access Modifiers in Java

The document discusses access modifiers in Java, which help determine the visibility and access privileges of class members. There are four access modifiers in Java - public, protected, private, and package-private (default if no modifier used). Access modifiers determine where within a package hierarchy and inheritance structure class members can be accessed, with public having the highest access and private the lowest.

Uploaded by

Shubhankar Singh
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)
140 views

Topics: Access Modifiers in Java

The document discusses access modifiers in Java, which help determine the visibility and access privileges of class members. There are four access modifiers in Java - public, protected, private, and package-private (default if no modifier used). Access modifiers determine where within a package hierarchy and inheritance structure class members can be accessed, with public having the highest access and private the lowest.

Uploaded by

Shubhankar Singh
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/ 14

Topics

• Access Modifiers in Java


(https://docs.oracle.com/javase/tutorial/java/j
avaOO/accesscontrol.html)

1 Object-Oriented Programming Using Java


What are Access Modifiers

• Helps the programmer to decide access levels for the


instance fields and methods of the class
• Encapsulation is supported via Access Modifiers
• Scope/Visibility/Access Privileges of the ‘Instance Fields’ and
‘Methods’ of a class is determined via their Access Modifiers
• Java Provides following
1. public (Highest Access/Privilege Level )
2. protected
3. private (Least Privilege Level)
4. package-private (Default Modifier – if no access modifier
is used)

Note: package-private is not a Java Keyword.


2 Object-Oriented Programming Using Java
A Simple Example
class Student
{
private String name; // name of student (Access Level : private)
int age; // age of student (Access Level: package-private)
protected String idno; // id number of student ( Access Level : protected)

public String getName()


{
return name;
}

public void display()


{
System.out.println(“Name :”+ name);
System.out.println(“Age : “+ age);
}

}// End of class Student


Decreasing Access Level
Highest Access Level Lowest Access Level

public protected package-private private

3 Object-Oriented Programming Using Java


Access Locations

• Places from where a member of class can be


accessed/referenced
• Five Possible Access Locations
1. From within a class itself
2. From other classes in the same package
3. From sub-classes defined with-in the same package
4. From sub-classes defined in some other packages
5. From classes defined in other packages

4 Object-Oriented Programming Using Java


Access Locations : Example
Consider the Following Package Hierarchy
PackA

B1,B2,B3 PackB PackX X1,X2,X3

PackC PackD PackY PackZ


C1,C2 D1,D2 Y1,Y2 Z1,Z2

• B1,B2 and B3 are classes in package PackB. Similarly classes X1,X2 and X3 belongs
to package PackX. C1,C2 classes belongs to PackC. D1,D2 classes belongs to
PackD.Y1,Y2 classes belongs to PackY. Z1,Z2 and Z3 classes belongs to classes
PackZ.
• Assume class B3 (package PackB) is a sub-class of B1 (package PackB).
• Assume class Z2 (package PackZ) is a sub-class of B2 (package PackB).

5 Object-Oriented Programming Using Java


Access Locations : Example

B1 <<super class>> B2 <<super class>>

B3 <<sub class>> Z2 <<sub class>>

B1 and B3 belongs to B2 belongs to Package Pack B


Same Package PackB Z2 belongs to Package PackZ

6 Object-Oriented Programming Using Java


Visibility of Fields/Methods
Access Modifiers

public protected package- private private


Access Locations

With in the Same Class

Sub-Classes in same package


Other Classes in same package
Subclasses in other packages
Non-subclasses in other packages

7 Object-Oriented Programming Using Java


Visibility of Fields/Methods

Access Modifiers

public protected package- private private


Access Locations

With in the Same Class Yes Yes Yes Yes

Sub-Classes in same package


Other Classes in same package
Subclasses in other packages
Non-subclasses in other packages

8 Object-Oriented Programming Using Java


Visibility of Fields/Methods

Access Modifiers

public protected package- private private


Access Locations

With in the Same Class Yes Yes Yes Yes

Sub-Classes in same package Yes Yes Yes No


Other Classes in same package
Subclasses in other packages
Non-subclasses in other packages

9 Object-Oriented Programming Using Java


Visibility of Fields/Methods
Access Modifiers

public protected package- private private


Access Locations

With in the Same Class Yes Yes Yes Yes

Sub-Classes in same package Yes Yes Yes No


Other Classes in same package Yes Yes Yes No
Subclasses in other packages
Non-subclasses in other packages

10 Object-Oriented Programming Using Java


Visibility of Fields/Methods
Access Modifiers

public protected package- private private


Access Locations

With in the Same Class Yes Yes Yes Yes

Sub-Classes in same package Yes Yes Yes No


Other Classes in same package Yes Yes Yes No
Subclasses in other packages Yes Yes No No
Non-subclasses in other packages

11 Object-Oriented Programming Using Java


Visibility of Fields/Methods
Access Modifiers

public protected package- private private


Access Locations

With in the Same Class Yes Yes Yes Yes

Sub-Classes in same package Yes Yes Yes No


Other Classes in same package Yes Yes Yes No
Subclasses in other packages Yes Yes No No
Non-subclasses in other packages Yes No No No

12 Object-Oriented Programming Using Java


Access Modifiers : Summary

• <<private> members of a class say ‘C’ are visible only in


side the class ‘C’
• <<package-private>> members of class ‘C’ are visible in
the all the classes of the package to which the class ‘C’
belongs
• <<protected>> members of class ‘C’ are visible in (i) all the
classes of the package to which the class ‘C’ belongs and
(ii) all the sub-classes of ‘C’
• <<public>> members of class ‘C’ are visible to every other
class belonging to the same or any other package
Decreasing Access Level
Highest Access Level Lowest Access Level

public protected package-private private


13 Object-Oriented Programming Using Java
Thank You

14 Object-Oriented Programming Using Java

You might also like