Java Object and Class
In Java, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity. Classes are used to create and manage objects and help to support inheritance.
An object is a collection of related data/functionality. Objects are created to share some behaviour. Objects can be created to call non-static methods which are not presented inside the Main Method but present inside the class.
Object
An object is the building block of the object-oriented programming language. An object is a real-world entity with its own attributes, behaviour and identity.
Real World Examples for objects
Characteristics of object
An object has three characteristics:
- STATE: It represents the data or value of an object.
- BEHAVIOUR: It represents the functionality or methods of an object.
- IDENTITY: Each object has a unique identity. Each object is uniquely identifiable.
Example of an Object
How to initialise the object in the class
Objects can be initialised in three ways.
Class
In Java, A class is a fundamental building block of Object-oriented programming(OOP) language. A class can define the structure, behaviour and state of an Object.
A class can be defined in two ways :
- Class expression.
- Class declaration.
How to declare a class in Java
We can declare a class using the “class” keyword in front of the class name, followed by the curly braces.
Class declaration
Public class Dataflair{ // body of the class(field and method) }
Create a class
Create a class named ‘DataFlair” with String S public class DataFlair { int x =6; In this example, we will create a class named with “DataFlair”
Create an object
Create an object called “obj” and print the value of S public class DataFlair { int x = 6; public static void main(String[] args) { DataFlair obj = new DataFlair(); System.out.println(obj.x); } }
Output:
6
In the above program, we have created a class named “DataFlair,” which contains a variable x. Inside a class, we have created an object named “obj”, and then we use the objects to call the methods of a class.
Multiple objects
We can create more than one object in one class. For example:
class DataFlair{ Int x = 6; int y = 8; String s = “Data Flair”; Public static void main(String args[]) { DataFlair obj1 = new DataFlair(); DataFlair obj2 = new DataFlair(); DataFlair obj3 = new DataFlair(); System.out.println(obj1.x); System.out.println(obj2.y); System.out.println(obj3.s); } }
Output :
6
8
Data Flair
In the above program, we have created a class named “DataFlair.” it contains 3 variables: x, y and s. Inside a class, we have created an object named “obj1”, “obj2”, and “obj3.” Then, we used the objects to call the methods of a class.
Difference between classes and object
CLASS | OBJECT |
A template for creating objects within a program. | An instance of a class. |
Logical entity. | Physical entity. |
Declared with the “class” keyword. | Created using the “new” keyword |
A class is declared once. | Multiple objects are created using a class. |
A class does not have memory when it is created. | Objects get memory when they are created. |