Class and Object in Java with Real Time Example and Program

Class and Object in Java with Real Time Example and Program

Objects and classes are the elementary building blocks of the object-oriented programming system. In this tutorial, we will learn about how classes and objects make the foundational components of the OOP system.

It also covers class and object in Java with real time example along with programming.

What is a class?

A class is considered to be the template which is used to create a group of similar objects. In other words, a class defines what data the object will contain and how it will behave.

The basic syntax used for creating a class is as follows:

class <class_name>
	data members;	//hold the data or information
	member functions; //determine the behaviour

For example:

class Car {
	String model;						
	String colour;	//data members
	void startCar() {
	    System.out.println(“Car Starts !”);
	void changeGears(){ //member function
    	    System.out.println(“Gears changed !”);

What is an object?

An entity with some state and behavior is referred to as an object. Objects can be tangible or intangible.

Object Examples: pen, car, bike, table, chair, mobile, etc.

The characteristics an object defines include state, behavior, and identity. The state of an object refers to the basic data it represents, behavior represents the functionality of the object and identity is the unique identification of the object.

Let’s consider a smartphone as an object. It is manufactured by Motorola, model name is G6, color is indigo black are regarded as the state of the smartphone. It is used to make calls, click wonderful pictures, and so these become the functionalities it provides.

Now, let’s have a look at how classes and objects are different with the help of a detailed example.

Difference between Class and Object in Java:

Consider a car and remodel it into a software entity. A car can have characteristics like color, model, mileage and can perform actions like change gears, start the car, stop the car.

So far, we have identified the following things:

Class Name: Cars
Data Members: color, model, mileage
Member Functions: start, change gears, stop.

Sample Program for Object and Class In Java:

Now, for the varied features, varied car objects can be created. Have a look at how this can be achieved with the help of java classes and objects.

public class Cars {
	String colour;
	String model;
	float mileage;
	public void startCar(){
            System.out.println(“Car starts !”);

	public void changeGears(){
	    System.out.println(“Gears changed !”);

	public void stopCar(){
	    System.out.println(“Car stopped !”);
	public String getInfo() {
            return (“Model:” +model + “ Colour:” + colour + “ Mileage:” +mileage);
	public static void main (String args[]){
		Cars Suzuki = new Cars();
		Suzuki.model = “Baleno”;
		Suzuki.colour = “Jet Black”;
		Suzuki.mileage = 27.39;


Model: Baleno Colour: Jet Black  Mileage: 27.39
Car starts!

That’s is all about class and object in Java with real time example.

To sum it up, classes are logical entities that define the blueprint from which objects are created and objects are physical entities with some particular state and behavior. So try practicing some examples of classes and objects in Java to understand them better.

Any question, before we move to next chapter of our Java tutorial?


  1. Thanks for this simple explanation. I’m going through these Java tutorials. I find them very simple and easy to understand.

    1. Thank you, Josette. Glad to know that the Java tutorials on are helping you in easily understanding the concepts.

Leave a Reply

Your email address will not be published. Required fields are marked *