Classes and Objects in Java

9
0

Introduction

Classes and objects are the basic building blocks of object-oriented programming in Java. A class defines the data and behaviour of an entity, while an object represents an actual instance of that class.

What Is a Class in Java?

A class in Java is a blueprint or template used to create objects. It describes the fields that store data and the methods that define behaviour.

A class body can contain fields, constructors, and methods.

For example, a Student class can contain fields such as name and age, along with a method for displaying student details.

Features of a Class

  • A class is a blueprint for creating objects.

  • It can contain fields, methods, and constructors.

  • It groups related data and behaviour.

  • One class can be used to create multiple objects.

  • It supports code organization and reusability.

How to Create a Class in Java

The class keyword is used to declare a class.

// Declare a class named Student
class Student {
// Define data members to store student information
String name = "Aman";
int age = 20;
// Define a method to display the student's details
void displayDetails() {
System.out.println(name + " is " + age + " years old.");
}
}

In this example:

  • Student is the class name.

  • name and age are fields.

  • displayDetails() is a method.

By convention, a Java class name begins with an uppercase letter.

Classes in java

Classes in java

What Is an Object in Java?

An object in Java is an instance of a class. It contains its own data and can use the methods defined inside the class.

Objects are generally created using the new keyword. The new operator creates the object and returns a reference to it.

Features of an Object

  • An object is created from a class.

  • It represents an actual entity.

  • It has a state represented by fields.

  • It has behaviour represented by methods.

  • Different objects can store different field values.

How to Create an Object in Java

The following program creates an object of the Student class:

public class Main {
public static void main(String[] args) {
// Create an object of the Student class
Student student1 = new Student();
// Access the object's data member using the dot (.) operator
System.out.println(student1.name); // Aman
// Call the object's method using the dot (.) operator
student1.displayDetails(); // Aman is 20 years old.
}
}

Here:

  • student1 is a reference variable.

  • new Student() creates a Student object.

  • The dot operator (.) accesses the object’s fields and methods.

Objects in java

Objects in java

Difference Between Class and Object in Java

Class

Object

A class is a blueprint or template.

An object is an instance of a class.

It defines fields and methods.

It stores actual field values and uses methods.

It is declared using the class keyword.

It is generally created using the new keyword.

One class can create many objects.

Each object can have its own state.

Summary

A class in Java is a blueprint that defines the data and behaviour of an entity using fields, methods, and constructors. It helps organize and reuse related data and functionality.

An object is an instance of a class that stores actual data and can access the class methods. Objects are generally created using the new keyword, allowing multiple objects of the same class to have different states.

CS Core

Read Similar Blogs

Comments0