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

Object-Oriented Programming (CS F213) : BITS Pilani

The document discusses access modifiers in Java. It explains that access modifiers determine the visibility and accessibility of fields and methods in a class. The four access modifiers in Java are public, protected, package-private (default), and private, with public having the highest access and private the lowest. It provides examples of which access levels can access fields and methods from different locations, such as within the same class, subclasses in the same package, and subclasses in other packages.

Uploaded by

SAURABH MITTAL
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)
81 views

Object-Oriented Programming (CS F213) : BITS Pilani

The document discusses access modifiers in Java. It explains that access modifiers determine the visibility and accessibility of fields and methods in a class. The four access modifiers in Java are public, protected, package-private (default), and private, with public having the highest access and private the lowest. It provides examples of which access levels can access fields and methods from different locations, such as within the same class, subclasses in the same package, and subclasses in other packages.

Uploaded by

SAURABH MITTAL
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/ 15

Object-Oriented Programming (CS F213)

Module I: Object-Oriented and Java Basics


CS F213 RL 3.2: Access Modifiers in Java

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 3.2 : Topics

Access Modifiers in Java


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

2 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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.


3 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
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

4 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

5 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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).

6 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

7 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

8 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

9 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

10 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

11 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

12 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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

13 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


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


14 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Thank You

15 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas

You might also like