Object & Class in Java
Imagine you have a box. This box is special because it can hold different things like toys, books, or anything you like. In programming, we have something similar called a “class.”
1. What is a Class?
A class is like a blueprint or plan for creating objects. Imagine you have a blueprint for building a house. It tells you what the house should look like and what rooms it should have. Similarly, a class tells the computer what properties and actions an object should have when it’s created.
Structure of Class
// Define a class
[access_modifier] class ClassName {
//Execution code like methods, fields, properties, constructor, etc.
}
2. What is an Object?
Now, imagine using that blueprint to create an actual car. That car you create is called an “object.”
An object is a real instance or copy of a class. It has unique properties (like the color of the car) and behaviors (like driving or stopping).
Structure of Object
// Creating an object
ClassName objectName = new ClassName(parameters);
// Accessing fields
objectName.fieldName = value;
// Calling methods
objectName.methodName();
Example: Car Class and Objects
public class Car {
public String color;
public int speed;
public void drive() {
System.out.println(“The car is driving.”);
}
public void stop() {
System.out.println(“The car has stopped.”);
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = “Red”;
myCar.speed = 60;
Car anotherCar = new Car();
anotherCar.color = “Blue”;
anotherCar.speed = 50;
myCar.drive();
anotherCar.stop();
}
}
Explanation
//Define a simple class for the Car
public class Car {
// Properties (attributes) of the car
public String color;
public int speed;
// Methods (actions) the car can do
public void drive() {
System.out.println(“The car is driving.”);
}
public void stop() {
System.out.println(“The car has stopped.”);
}
public static void main(String[] args) {
// Let’s create objects (instances) of the Car class:
Car myCar = new Car(); // Creating a new car object
myCar.color = “Red”; // Setting the color of the car
myCar.speed = 60; // Setting the speed of the car
Car anotherCar = new Car(); // Creating another car object
anotherCar.color = “Blue”; // Setting color
anotherCar.speed = 50; // Setting speed
// Using the methods of the car objects
myCar.drive(); // Output: The car is driving.
anotherCar.stop(); // Output: The car has stopped.
}
}
Task:
1. Create a class named Employee with the following properties:
○ String name
○ int id
○ double salary
Create an object of the Employee class in the main method. Assign values to the properties and print them to the console.
2. Create a class named Product with the following properties:
○ String productName
○ String category
○ double price
Create an object of the Product class in the main method. Assign values to the properties and print them to the console.
3. Create a class named Student with the following properties:
○ String name
○ int rollNumber
○ String course
Create an object of the Student class in the main method. Assign values to the properties and print them to the console.
4. Create a class named Car with the following properties:
○ String make
○ String model
○ int year
Create an object of the Car class in the main method. Assign values to the properties and print them to the console.
5. Create a class named Book with the following properties:
○ String title
○ String author
○ int pages
Create an object of the Book class in the main method. Assign values to the properties and print them to the console.
Course Video
YouTube Reference :
This training focuses on understanding Java classes and objects, their structure, and usage in real-world applications..
Yes, this course is designed for complete beginners to help them grasp the fundamentals of Java classes and objects.
The training covers the basics of Java classes and objects, including their structure and purpose. It explains fields and methods, constructors, and how to create and use objects effectively. Additionally, it provides practical examples of real-world objects in Java to help learners understand and apply these concepts.
You will learn how to define Java classes, understand fields and methods, and use access modifiers effectively.
No, prior programming knowledge is not required.
You will learn to create and use Java classes and objects, write object-oriented programs, and implement constructors and methods effectively.
The training is self-paced, allowing you to learn at your convenience.
Yes, this is a free course. While certificates aren’t provided, you will gain valuable skills in Java programming and object-oriented principles.