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

AccessSpecifiers

The document explains access specifiers in Java, which are essential for encapsulation in object-oriented programming. It outlines four access specifiers: public, protected, default (package-private), and private, detailing their accessibility levels and use cases. Each specifier defines the visibility of classes, methods, and variables, influencing how they can be accessed within and outside their defining classes.

Uploaded by

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

AccessSpecifiers

The document explains access specifiers in Java, which are essential for encapsulation in object-oriented programming. It outlines four access specifiers: public, protected, default (package-private), and private, detailing their accessibility levels and use cases. Each specifier defines the visibility of classes, methods, and variables, influencing how they can be accessed within and outside their defining classes.

Uploaded by

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

Access Specifiers

(Visibility Modes)
Access Specifiers
 One of the techniques in object-oriented
programming is encapsulation.
 It concerns the hiding of data in a class
and making this class available only
through methods. In this way the
chance of making accidental mistakes
in changing values is minimized.
 Access Specifier defines the
boundary and scope to access the
method, variable and class
Access Specifiers
 Java offers four access specifiers,
listed below in decreasing
accessibility:
 public
 protected
 default (no specifier)
 private
Access Specifier -
public
 public classes, methods, and fields can be
accessed from everywhere.

 Fields, methods and constructors declared


public within a public class are visible to any
class in the Java program, whether these
classes are in the same package or in another
package.

 You use public classes, methods, or fields only


if you explicitly want to offer access to these
entities and if this access cannot do any harm.
Access Specifier -
default
 Java provides a default specifier which is
used when no access specifier is present.
 Any class, field, method or constructor that
has no declared access specifier is
accessible only by classes in the same
package.

 It is also called as friendly or package


friendly, package private.
Access Specifier-private
 If you declare any variable /function
using private access specifier then it
can be used within the java class itself.
 private methods and fields are not
visible within subclasses and are not
inherited by subclasses.
 It is mostly used for encapsulation: data
are hidden within the class and can be
accesses only through methods
provided.
 Class cannot be declared as private
Access Specifier -
protected
 If you want to access the
variable/functions of parent class within
the child class then you can declare those
variable as protected .
 If your variable/method is declared as
protected then it can be accessed by the
same class, sub classes and classes from
same package.
 Class cannot be declared as protected
Subclass
Clas Packag World
Specifier (other
s e (outside package)
package)

public Y Y Y Y

protected
(subclass in other Y Y Y N
package)
No
specifier/default/frie
Y Y N N
ndly/
package private

private Y N N N

You might also like