Unit-2: Object
Unit-2: Object
Object
An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only.
For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It is used to write, so writing
is its behavior.
An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object
is the instance (result) of a class.
Object Definitions:
For example
Bicycle is an object
Java Class
Before you create objects in Java, you need to define a class. A class is a blueprint for the object.
We can think of the class as a sketch (prototype) of a house. It contains all the details about the
floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
Since many houses can be made from the same description, we can create many objects from a
class.
o Fields
o Methods
o Constructors
o Blocks
o Nested class
class ClassName {
// variables
// methods
}
For Example
class Lamp {
// instance variable
boolean isOn;
// method
void turnOn() {
isOn = true;
}
// method
void turnOff() {
isOn = false;
}
}
Java Objects
An object is called an instance of a class. For example, suppose
Animal is a class then cat, dog, horse and so on can be considered as object of animal class.
// create object
Lamp l1 = new Lamp();
class Lamp {
boolean isOn;
}
// create object
Lamp l1 = new Lamp();
void turnOn() {
// initialize variable with value true
isOn = true;
System.out.println("Light on? " + isOn);
void turnOff() {
// initialize variable with value false
isOn = false;
System.out.println("Light of? " + isOn);
}
}
class Main {
public static void main(String[] args) {
Output
1. By reference variable
2. By method
3. By constructor
Initializing an object means storing data into the object. Let's see a simple example where we are
going to initialize the object through a reference variable.
class Student{
int id;
String name;
}
class TestStudent{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Corona";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
}
}
Output:
1o1 corona
We can also create multiple objects and store information in it through reference variable.
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="prakash";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
O/p
101 prakash
102 Amit
In this example, we are creating the two objects of Student class and initializing the value to
these objects by invoking the insertRecord method. Here, we are displaying the state (data) of the
objects by invoking the displayInformation() method.
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation()
{System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"MS Dhoni");
s1.displayInformation();
s2.displayInformation();
}
}
O/p
111 Karan
222 MS Dhoni
3) Object and Class Example: Initialization through a constructor
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display()
{System.out.println(id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}
O/p
In object-oriented programming, the method is a jargon used for function. Methods are bound to a class and they
define the behavior of a class.
class method {
// method call
myMethod();
// method definition
void myMethod(){
System.out.println("I am inside of method definition so printing from inside myMethod()!");
}
}
Output:
Method encountered.
I am inside of method definition so Printing from inside myMethod().
Method was executed successfully!
Let's see another example,
class method {
class Output {
Output:
About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class.
We can change the access level of fields, constructors, methods, and class by applying the access modifier
on it.
1. Private: The access level of a private modifier is only within the class. It cannot be accessed from
outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be accessed
from outside the package. If you do not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and outside the package
through child class. If you do not make the child class, it cannot be accessed from outside the
package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within the
class, outside the class, within the package and outside the package.
In c++, classes belong to name space, just like a library/package and in java, classes belong to
package.
1) Private
The private access modifier is accessible only within the class.
In this example, we have created two classes A and Simple. A class contains private data
member and private method. We are accessing these private members from outside the class, so
there is a compile-time error.
Class A{
private int data=40;
private void msg()
{System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[])
{
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
2) Default
If you don't use any modifier, it is treated as default by default. The default modifier is accessible only
within package. It cannot be accessed from outside the package. It provides more accessibility than
private. But, it is more restrictive than protected, and public.
In this example, we have created two packages pack and mypack. We are accessing the A class from
outside its package, since A class is not public, so it cannot be accessed from outside the package.
class A{
void msg()
{System.out.println("Hello");}
}
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
3) Protected
The protected access modifier is accessible within package and outside the package but through
inheritance only.
The protected access modifier can be applied on the data member, method and constructor. It can't be
applied on the class.
public class A{
protected void msg()
{System.out.println("Hello, I am Protected case");}
}
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
4) Public
The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
public class A{
public void msg()
{System.out.println("Hello, I am public one");}
}
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
}
class TestThis1{
public static void main(String args[])
{
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
O/P
0 null 0.0
0 null 0.0
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
O/P
111 ankit 5000
112 sumit 6000
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
O/P
111 ankit 5000
112 sumit 6000
Java Inner Classes
In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together,
which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and then create an object of the inner class:
Example
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
System.out.println(myInner.y + myOuter.x);
// Outputs 15 (5 + 10)
Private Inner Class
Unlike a "regular" class, an inner class can be private or protected. If you
don't want outside objects to access the inner class, declare the class
as private:
Example
class OuterClass {
int x = 10;
int y = 5;
System.out.println(myInner.y + myOuter.x);
If you try to access a private inner class from an outside class (MyMainClass), an error occurs
class OuterClass {
int x = 10;
int y = 5;
System.out.println(myInner.y);
// Outputs 5
Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.
Example
class OuterClass {
int x = 10;
class InnerClass {
return x;
}
public class MyMainClass {
System.out.println(myInner.myInnerMethod());
// Outputs 10
Age is: 30
Name is: Steve
O/P
111 Karan ITS
222 Aryan ITS