Class and Object in Java Example

Class and Object in Java Example

Here you will get the basic program code to implement the concept of class and object in java example.

Difference between Class and Object in Java?

Class

  • Think of a class as a blueprint or a recipe.
  • It’s like a plan that tells you how to create something.
  • In Java, a class defines what an object will be like. It’s a way to describe an object’s characteristics and behavior.
  • Imagine you’re making cookies: the recipe (class) tells you what ingredients you need and how to combine them.

Example

 

Object

  • An object is like the real thing you create using the blueprint (class).
  • It’s the actual instance, the physical cookie you get from following the recipe.
  • In Java, an object is like a container that holds data and can do things (methods).
  • For example, if the class is “Car,” an object could be “MyCar,” with its own specific details like color, brand, and year.

Example

Class and Object in Java Example

Program Explanation

When you run this program, it will create two car objects, set their attributes, and display information about each car, demonstrating the concept of classes and objects in Java.

Output

C:\CodeRevise\java>javac mainClass.java

C:\CodeRevise\java>java mainClass
Car 1 Deatils:
Brand: TATA
Model: Harrier
Year: 2023

Car 2 Details:
Brand: Mahindra
Model: Scorpio
Year: 2022

 

 

 

 

Leave a Comment

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

Scroll to Top