Aim: Program on class and objects
Objective: To Understands and implement a simple java program using classes
and objects to learn how java treats data and behaves in an object oriented
manner.
Tools : Eclipse, Notepad++, Java(JPK)
THOERY:
1)What is class in Java?
A class in Java is a template that defines the structure and behavior of objects. It contains variables
(attributes) and methods (functions) to describe what an object is and what it can do.
2)What is object in Java?
In Java, an object is a specific instance of a class created during the program's execution. It holds
actual values in its fields, giving it a unique state. For example, if you create a Dog object with a name
and age, that specific dog has its own identity and behavior. You can then make it perform actions like
barking or running by calling its methods. Objects are stored in memory and are essential to how Java
programs run, allowing code to be organized in a way that mirrors real-world concepts.
3)Why do we use classes and objects in Java?
We use classes and objects in Java to make programs modular, organized, and closer to how things
work in the real world. Eg A class acts like a blueprint—it defines what something is and how it behaves.
For example, you can define a class called Car that describes properties like color and speed, and
actions like driving or braking. Then, using that blueprint, you create objects like myCar or yourCar,
each with their own specific details but based on the same design.
Syntax:
class Car {
String brand;
String model;
int year;
Car(String brand, String model, int year) {
[Link] = brand;
[Link] = model;
[Link] = year;
void displayInfo() {
[Link]("Car: " + brand + " " + model + " (" + year + ")");
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Honda", "Civic", 2021);
[Link]();
Output:
Car: Honda Civic (2021)