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

Oop 5 - Access Modifiers

Access modifiers in Java specify the visibility and accessibility of classes, methods, and variables. There are four access modifiers: public, protected, default (no modifier), and private. Public members are visible everywhere, private members are only visible within the same class, protected is visible within the same package and subclasses, and default is visible only within the same package. Access modifiers determine where classes, methods, and variables can be accessed and used within a program.

Uploaded by

kasun Herath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Oop 5 - Access Modifiers

Access modifiers in Java specify the visibility and accessibility of classes, methods, and variables. There are four access modifiers: public, protected, default (no modifier), and private. Public members are visible everywhere, private members are only visible within the same class, protected is visible within the same package and subclasses, and default is visible only within the same package. Access modifiers determine where classes, methods, and variables can be accessed and used within a program.

Uploaded by

kasun Herath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

IS 12308-Object Oriented

Programming
Lecture 5 – Access
Modifiers

By
Upeksha Kudagamage
Lecturer-SUSL
Access Modifiers
⚫ The access modifiers in java specifies accessibility (scope)
of a data member, method, constructor or class.
⚫ There are 4 types of java access modifiers:
⚫ private
⚫ default
⚫ protected
⚫ public

2
Access Modifier Visibility

Public Visible to all packages

protected Same package + sub classes

Default (No Access Same package only


Modifier)
private Same class only

3
⚫ Public:
Class, Variables, methods, Constructors

• Protected/ Private:
Variables, methods, constructors

Default:
Class, Variables, methods, Constructors
4
Packages
⚫ A Package can be defined as a grouping of related
types(classes, interfaces, enumerations.

⚫ Some of the existing packages in Java are:


java.lang - bundles the fundamental classes
java.io - classes for input , output functions are
bundled in this package.

Programmers can define their own packages to bundle group


of classes/interfaces, etc.

5
Package and Import
⚫ Classes can be organized into packages.

⚫ Classes should be imported before used in other package.

⚫ In a source file declarations should be;


import <package_name>.<class_name>

Name of the class you


are going to import

6
Public
⚫ The public access modifier is accessible
everywhere. It has the widest scope among all
other modifiers.

7
Protected
⚫ The protected access modifier is accessible within
package and outside the package but through
inheritance only.

⚫ The protected access modifier can be applied on the


data member, method and constructor. It can't be
applied on the class.

8
Ex:
⚫ we have created the two
packages pack and
mypack.
⚫ The A class of pack
package is public, so can
be accessed from outside
the package. But msg
method of this package is
declared as protected, so
it can be accessed from
outside the class only
through inheritance
9
Private
⚫ The private access modifier is accessible only within
class.

10
Default Access Modifier
⚫ If you don't use any modifier, it is treated as default by
default. The default modifier is accessible only within
package.

⚫ In this example, we have created two packages pack


and mypack. We are accessing the A class from
outside its package, since A class is not public, so it
cannot be accessed from outside the package.

11
Ex:

12
public vs. private
⚫ Classes are usually declared to be public
⚫ Instance variables are usually declared to be private
⚫ Methods that will be called by the client of the class
are usually declared to be public
⚫ Methods that will be called only by other methods of
the class are usually declared to be private

13
Access Levels

14
END

15

You might also like