Class and Object in Java

The major objective of the object-oriented approach is to eliminate some of the flaws encountered in the procedural approach. Oops treats data as a critical element and it does not allow data to flow freely around the system. It binds data more closely to the function and operate on it and protects it from unintentional modification by the other functions.

Object = Data + Method

The Data of an object can be accessed only by the methods associated with that object. However, the method of one object can access other objects.

Some of the features of the Object Oriented Programming are:

  • Emphasis on data rather than objects.

  • Programs are divided into what is known as Objects.

  • Data is hidden outside of its scope and can not be accessed by external resources.

  • Objects can communicate with each other with the help of methods.

  • New data and methods can be added easily when needed.

  • Follows the bottom-up approach in program design.

Basic concepts of Object Oriented Programming-

Objects are the real-world entity that describes the behavior of the class. The object is the thing from which we can perform our operation. The object can be an employee, a person, a Bank Account, an organization, an Address a table of data or any item that a program can handle. The object can also represent any user-defined data types such as List, String, Socket, etc. The object contains all the methods and behaviors of the class.

When a program gets executed objects may interact with each other by sending messages to one another. For example, an Employee object can interact with Account to check the bank balance by sending a message. Each object contains data and code to manipulate other object data. Objects can interact without knowing each other’s data or code.

Here, just mentioned that the object contains data and code to manipulate the data and the entire set of data and code of object can be made a user-defined data type using the concept of class. A class can be a data type and an object can be a variable of that data type. When a class is created, we can create any number of objects for that class. Each object will get the same behavior that is described in the class but data may differ, as the initialization technic may be different for each object.

For Example, the syntax of creating an object is the same as creating a variable in C. If Employee is been defined as a class then the declaration of the object of Employee class will be –

Employee emp = new Employee();

Create a Class

To create a class, use the keyword class:

Syntax

class <class_name>{  
    field;  
    method;  
}  

Java Class Example

public class Demo {
  int d = 10;

  public static void main(String[] args) {
    demo myObj = new demo();
    System.out.println(myObj.d);
  }
}
Example Output

Create an Object

To create an object of Demo, specify the class name, followed by the object name, and use the keyword new

Syntax

ClassName ReferenceVariable = new ClassName();

public class Demo {
  int d = 10;

  public static void main(String[] args) {
    demo myObj = new demo();
    System.out.println(myObj.d);
  }
}
Example Output

Multiple Objects

You can create multiple objects of one class:

public class Demo {
  int d = 10;

  public static void main(String[] args) {
    demo  myObj1 = new demo ();  // Object 1
    demo  myObj2 = new demo ();  // Object 2
    System.out.println(myObj1.d);
    System.out.println(myObj2.d);
  }
}
Example Output

Share