Object and Class in Java

Object and Class in Java

 

In this section we will learn about the objects and classes in Java programming language. A program in object oriented programming is created by using objects and classes. An object is an entity that has some state or behavior or that can be touched or that is logical.

 

Object in Java:

An object is an entity that has some state or behavior or that can be touched or that is logical. For example, a table, a pen, a chair, etc all is objects as they are in some state and they can be touched. An object can be physical or it can be logical. The best example of logical or intangible object is the banking system. An object has three characteristics.

 State:

The state of an object is used to represent the value or the data that is stored in the object.

 Behavior:

The behavior of an object represents the working or functionality of the object for example an object that is related to banking can be used to withdraw money or to deposit money.

Identity:

The identity of an object is a unique ID. This unique ID and the value of the unique ID are not visible to external users. The ID of the object is used internally by JVM to identify the object.

Consider the example that a pen is an object and its name is dollar and its color is red, which is its state and it is used to write which is the behavior of the pen.

Object is an instance of a class:

An object in object oriented programming is a result of a class or it is created in a class therefore, we can refer an object as the instance of a class. A class is a template.

 

Class in Java:

A ‘class’ in Java object oriented programming language is a group of objects and these objects have same properties. A class is considered as a template which creates objects. The following are the parts that a class has in object oriented programming languages.

data member

method or function

constructor

block

class and inheritance

 

Syntax to declare a class in Java:

The following is the syntax to declare a class in java programming language:

class <name of class> {

data member;

method;

}

In the syntax class is the keyword which is used to declare the class and <name of class> will be name that will be given by the user to class. Data member is the variables that will be declared for functions. Method is the function that will perform certain specific task.

 

Simple Example of Object and Class:

Consider the following example in which we have declared a class with its data members and methods, objects of class are made by using the keyword ‘new’:

CODE:

class Employee {

int id;

string name;

public static void main (string [] args) {

Employee e = new Employee ();

system. out. println (e. id);

system. out. println (e. name);

}

}

OUTPUT:

0

null

In the above example, a class named ‘Employee’ is declared and it has two data members that are id and name. Then in the main method an object is created of the class ‘Employee’ with the keyword ‘new’ and named ‘e’.

Using the object ‘e’, we printed values of id and name, the data members, of the class. In the above example it can be seen that the value that is printed of id is 0 this is because we did not initialized the variable id and also for the data member name we got null because we did not initialized it.

 

Instance Variable in Java:

An instance variable is a variable that is declared inside the class but not in the method or it is declared outside the method. When an object is created at run time then the instance variable gets the memory. In other words we can say that instance variable does not get the memory at the time of compilation of the program.

 

Method in Java:

In Java programming language, a method is referred as a function which is used to show the working of an object.

Advantage of method:

There are two main advantages of method in object oriented programming language:

  1. Code reusability
  2. Code optimization

By code reusability we mean that we can reuse the code that has methods in object oriented programming. Code optimization is referred as code modification which improves the quality and efficiency of the code.

 

new keyword:

The keyword ‘new’ is used to allocate the memory to the object at run time.

 

Example of Object and class that maintains the records of employee:

Consider the following example in which we declared a class and the code is used to maintain the record of employees:

CODE:

class Employee {

int no;

string name;

void insertRecord (int n, string n1) {

no = n;

name = n1;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee e1 = new Employee ();

Employee e2 = new Employee ();

e1. insertRecord (121, “Stuart”);

e2. insertRecord (122, “Bill”);

e1. display ();

e2. display ();

}

}

OUTPUT:

121 Stuart

122 Bill

In the above example, a class named ‘Employee’ is declared that has two data members and two methods or member functions. The first method that is insertRecord () is used to enter the record of the student. The record will be passed from the main function by using the objects as function arguments that is n and n1. Here n is the number of the employee and n1 is the name of the employee. These arguments will be assigned to the data members then in the method.

Then comes the next method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

 

Another Example of Object and Class:

Consider the following example in which we have declared a class named rectangle that calculates the area of the rectangle:

CODE:

class Rectangle {

int length;

int width;

void insert (int l, int w) {

length = l;

width = w;

}

void area () {

system. out. println (length * width);

}

public static void main (string [] args) {

Rectangle r = new Rectangle ();

Rectangle r1 = new Rectangle ();

  1. insert (2, 3);

r1. insert (4, 5);

  1. area ();

r1. area ();

}

}

OUTPUT:

6

20

In the above example the class rectangle has two data members that are length and width and two methods. The first method is ‘insert’ which is used to insert the data and the second method is used to calculate the area of the rectangle and display it. In the main function two objects are created r and r1that are used to call the methods or member functions of the class.

 

What are the different ways to create objects in Java?

The following are the ways through which we can create objects in Java programming language:

  1. Using the keyword ‘new’
  2. Using newInstance () method
  3. Using clone () method
  4. Using factory method etc

 

Anonymous Object:

Anonymous means nameless something that has no name. In Java programming language by anonymous object we mean an object that has no reference. An anonymous object in Java programming language is created when we only want to use an object only once.

Consider the following example in which we have created an anonymous object to call a method of the declared class:

CODE:

class factorial {

void fact (int n) {

int fact = 1;

for (int j; j <= n; j++) {

fact = fact * j;

}

system. out. println (“The factorial of entered number is” + fact);

}

public static void main(string args []) {

new Factorial (). fact (4);

}

OUTPUT:

The factorial of entered number is 24

In the above example, the program is calculating the factorial of a number. We used the anonymous object to call the method of the class directly.

 

Creating multiple objects by one type only:

In Java programming language, multiple objects can be created by using only one type as more than one variable can be declared using only one data type for example:

int a, b, c;

Consider the following example in which we have calculated the area of the rectangle and created multiple objects by only one type:

CODE:

class Rectangle {

int length;

int width;

void insert (int l, int w) {

length = l;

width = w;

}

void area () {

system. out. println (length * width);

}

public static void main (string [] args) {

Rectangle r = new Rectangle (), r1 = new Rectangle ();

  1. insert (2, 3);

r1. insert (4, 5);

  1. area ();

r1. area ();

}

}

OUTPUT:

6

20

In the above example the class rectangle has two data members that are length and width and two methods. The first method is ‘insert’ which is used to insert the data and the second method is used to calculate the area of the rectangle and display it. In the main function two objects are created r and r1 that are used to call the methods or member functions of the class. These objects are created by only one type.