0% found this document useful (0 votes)
19 views14 pages

01-Mar-2021 Access Specifiers in Java

Uploaded by

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

01-Mar-2021 Access Specifiers in Java

Uploaded by

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

ACCESS

SPECIFIERS
IN
JAVA
ACCESS SPECIFIERS IN JAVA

● Java Access Specifiers (also known as Visibility Specifiers ) regulate access to


classes, fields and methods in Java

● These Specifiers determine whether a field or method in a class, can be used or


invoked by another method in another class or sub-class

● Access Specifiers can be used to restrict access

● Access Specifiers are an integral part of object-oriented programming


TYPES OF ACCESS SPECIFIERS

In java we have four Access Specifiers and they are listed below

1. public
2. private
3. protected
4. default(no specifier)
PUBLIC SPECIFIERS

● Public Specifiers achieves the highest level of accessibility

● Classes, methods, and fields declared as public can be accessed from any class in
the Java program, whether these classes are in the same package or in another
package.

Example :
public class Demo {
// public class public x, y, size;
// public instance variables
}
PRIVATE SPECIFIERS

● Private Specifiers achieves the lowest level of accessibility.private methods and


fields can only be accessed within the same class to which the methods and fields
belong

● private methods and fields are not visible within subclasses and are not inherited by
subclasses. So, the private access specifier is opposite to the public access
specifier

● Using Private Specifier we can achieve encapsulation and hide data from the
outside world
EXAMPLE

public class Demo { // public class


private double x, y; // private (encapsulated) instance
variables
public set(int x, int y) { // setting values of private
fields
this.x = x;
this.y = y;
}
public get() { // setting values of private fields
return Point(x, y);
}
}
PROTECTED SPECIFIERS

● Methods and fields declared as protected can only be accessed by the subclasses
in other package or any class within the package of the protected members' class

● The protected access specifier cannot be applied to class and interfaces


DEFAULT(NO SPECIFIER)

● When you don't set access specifier for the element, it will follow the default
accessibility level

● There is no default specifier keyword. Classes, variables, and methods can be


default accessed.Using default specifier we can access class, method, or field
which belongs to same package,but not from outside this package

Example : class Demo


{
int i; (Default)
}
EXAMPLE

package p1; import p1.*;


//Class Geeks is having Default access //This class is having default access
modifier modifier
class Geek { class GeekNew {
void display() { public static void main(String
System.out.println("Hello World!"); args[]) {
} //accessing class Geek from package
} p1
//default modifier Geeks obj = new Geek();
package p2;
obj.display();
}
}
REAL TIME EXAMPLE
ACCESS SPECIFIERS FOR CLASSES

● The class will be accessible to other classes in the same package but will be
inaccessible to classes outside the package

● When we say that a class is inaccessible, it simply means that we cannot create an
object of that class or declare a variable of that class type

● The protected access specifier too cannot be applied to a class


THANK YOU

You might also like