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

Characteristics of OBJECT and Access Modifiers

Uploaded by

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

Characteristics of OBJECT and Access Modifiers

Uploaded by

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

Object: An object is a basic unit of Object-Oriented Programming and represents real-world entities.

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.

represents the data (value) of an object.

It is represented by attributes of an object. It also reflects the properties of an object

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.

represents the behavior (functionality) of an object such as deposit, withdraw, etc.

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.

Types of Access Modifiers in Java

There are four types of access modifiers available in Java:

1. Private
2. Default – No keyword required
3. Protected
4. Public

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.

 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. }

Role of Private Constructor

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. }

Default Access Modifier

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:

// Java program to illustrate default modifier


package mypro;

// Class Test is having Default access modifier


class Test
{
void display()
{
System.out.println("Hello World!");
}
}

Program 2:
// Java program to illustrate error while using class from different package with
// default modifier

package mypro2;
import mypro1.*;

// This class is having default access modifier


Public class Test2
{
public static void main(String args[])
{
// Accessing class Geek from package p1
Test obj = new Test(); //Compile Time Error

obj.display(); //Compile Time Error


}
}

Output:

Compile time error

Protected Access Modifier

 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.

Let us consider an example for a protected 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.

So here we get the output as Hi I’m from a protected method.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy