Java Class Object Access Modifier
Java Class Object Access Modifier
JAVA Object
An object in Java is a basic unit of Object-Oriented
Programming and represents real-life entities.
Objects are the instances of a class that are created to use the
attributes and methods of a class.
A typical Java program creates many objects, which as you know,
interact by invoking methods.
An object consists of:
1. State: It is represented by attributes of an object. It also
reflects the properties of an object.
2. Behavior: It is represented by the methods of an object. It
also reflects the response of an object to other objects.
3. Identity: It gives a unique name to an object and enables one
object to interact with other objects.
Program 1:
// Java program to illustrate default modifier
package p1;
Program 2:
// Java program to illustrate error while
// using class from different package with
// default modifier
package p2;
import p1.*;
obj.display();
}
}
Output:
Compile time error
2. Private Access Modifier
The private access modifier is specified using the
keyword private.
The methods or data members declared as private are accessible
only within the class in which they are declared.
o Any other class of the same package will not be able to
access these members.
o Top-level classes or interfaces cannot be declared as private
because
private means “only visible within the enclosing class”.
protected means “only visible within the enclosing
class and any subclasses”
Hence these modifiers in terms of application to classes, apply
only to nested classes and not on top-level classes
In this example, we will create two classes A and B within the same
package p1. We will declare a method in class A as private and try to
access this method from class B and see the result.
Program:
// Java program to illustrate error while
// using class from different package with
// private modifier
package p1;
class A
{
private void display()
{
System.out.println("GeeksforGeeks");
}
}
class B
{
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
obj.display();
}
}
Output:
error: display() has private access in A
obj.display();
Program 1:
// Java program to illustrate
// protected modifier
package p1;
// Class A
public class A
{
protected void display()
{
System.out.println("GeeksforGeeks");
}
}
Program 2:
// Java program to illustrate
// protected modifier
package p2;
import p1.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
Output:
GeeksforGeeks
Program 1:
// Java program to illustrate
// public modifier
package p1;
public class A
{
public void display()
{
System.out.println("GeeksforGeeks");
}
}
Program 2:
package p2;
import p1.*;
class B {
public static void main(String args[])
{
A obj = new A();
obj.display();
}
}
Output:
GeeksforGeeks
Algorithm to use access modifier in Java
Here’s a basic algorithm for using access modifiers in Java:
Define a class: Create a class that represents the object you
want to manage.
Define instance variables: Within the class, define instance
variables that represent the data you want to manage.
Specify an access modifier: For each instance variable, specify an
access modifier that determines the visibility of the variable. The
three main access modifiers in Java are private, protected, and
public.
Use private for variables that should only be accessible
within the class: If you want to prevent access to a variable
from outside the class, use the private access modifier. This is
the most restrictive access modifier and provides the greatest
level of encapsulation.
Use protected for variables that should be accessible within
the class and its subclasses: If you want to allow access to a
variable from within the class and its subclasses, use the
protected access modifier. This is less restrictive than private and
provides some level of inheritance.
Use public for variables that should be accessible from
anywhere: If you want to allow access to a variable from
anywhere, use the public access modifier. This is the least
restrictive access modifier and provides the least amount of
encapsulation.
Use accessor and mutator methods to manage access to the
variables: In order to access and modify the variables, use
accessor (getter) and mutator (setter) methods, even if the
variables have a public access modifier. This provides a level of
abstraction and makes your code more maintainable and
testable.
In this example, we will create two packages and the classes in
the packages will be having the default access modifiers and we
will try to access a class from one package from a class of the
second package.