Characteristics of OBJECT and Access Modifiers
Characteristics of OBJECT and Access Modifiers
Objects are the instances of a class that are created to use the attributes and methods of a class.
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc.
It can be physical or logical (tangible and intangible). The example of an intangible object is the banking
system.
Characteristics of an object
All individual objects possess three basic characteristics -- identity, state and behavior.
Identity means that each object has its own object identifier and can be differentiated from all
other objects. Each object's name, or identity, is unique and distinct from other objects. It enables
one object to interact with other objects.
An object identity is typically implemented via a unique ID. The value of the ID is not visible to
the external user. However, it is used internally by the JVM to identify each object uniquely
State refers to the properties of an object. For example, values of variables in the object contain
data that can be added, changed or deleted.
The state controls aspects of an object; in the case of describing a fan, you could have an on, off, low,
medium, or high state. State monitors behavior, such as turning a fan on or off, the state will change
when the behavior happens.
Behavior refers to actions that the object can take. For example, one object can respond to
another object to carry out software functions.
Object behavior is used to describe what an object can do, such as a fan turning on or off or changing
speeds.
It is represented by the methods of an object. It also reflects the response of an object with other
objects.
Identity
The identity is a characteristic used to uniquely identify that object – such as a random ID number or an
address in memory. Simpler objects like a lighter may have only two states (on and off) and behaviors
(turn on, turn off), but they still have an identity (that item’s manufacturing ID, for example).
Access Modifiers in Java
The access modifiers are the concept of object-oriented programming. The access modifiers are used to
specify the accessibility or scope of classes, constructors, methods, data members and other members.
Using the access modifiers we can change the access level of these classes, methods, constructors, and
other members.
1. Private
2. Default – No keyword required
3. Protected
4. Public
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.
Any other class of the same package will not be able to access these members.
Top-level classes or interfaces can not be declared as private because
o private means “only visible within the enclosing class”.
o 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.
Example-1.
package project1;
class A
{
private void display()
{
System.out.println("Hello Students");
}
}
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();
Example-2
1. package project1;
2. class A{
3. private int data=40;
4. private void msg(){System.out.println("Hello Students");}
5. }
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
If we make any class constructor private, we cannot create the instance of that class from outside the
class. Hence, we can conclude that the private access modifier can be accessed only within the same
class and not from outside the class.
For example:
1. class A{
2. private A(){}//private constructor
3. void msg(){System.out.println("Hello java");}
4. }
5. public class Simple{
6. public static void main(String args[]){
7. A obj=new A();//Compile Time Error
8. }
9. }
When no access modifier is specified for a class, method, or data member – It is said to be having the
default access modifier by default. The data members, classes, or methods that are not declared using
any access modifiers i.e. having default access modifiers are accessible only within the same package.
It cannot be accessed from outside the package. It provides more accessibility than private. But, it is
more restrictive than protected, and public.
Program 1:
Program 2:
// Java program to illustrate error while using class from different package with
// default modifier
package mypro2;
import mypro1.*;
Output:
It is a keyword. This access modifier is used to access the methods or data members of a class
within the same package as well as outside the package but only through inheritance. The
protected access modifier has more accessibility than private and defaults access modifiers. But
it has less visibility than the public access modifier.
Here we have two packages p1 and p2. In package p1 we have class A1 where we have declared a
protected test method. In package p2 we are inheriting the members of class A1 inside class A2 with
help of extending keywords and creating a relationship between the two classes. We can also say that
class A1 is the parent class or the superclass and class A2 is the child class or the subclass respectively.
When we inherit the members of class A1 inside class A2, with the help of a protected access modifier
we can access the members of class A1 of package p1 from class A2 of the different package p2.
Hence, we can conclude that the methods, variables, and data members of a class prefixed with a
protected access modifier can be accessed within the same package as well as can be accessed from
outside the package but only with the help of inheritance.
Program 1:
// Java program to illustrate
// protected modifier
package p1;
// Class A
public class A
{
protected void display()
{
System.out.println("Hello Students");
}
}
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:
Hello Students