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.”
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 a Class
// Define a class
[access_modifier] class ClassName {
//Execution code like methods, fields, properties, constructor, etc.
}
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 an 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.
}
}
Tasks:
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 :
The training covers the Java Collections Framework, including its concepts, syntax, and practical applications.
Yes, it includes examples demonstrating how to use collection classes like ArrayList
, HashSet
, LinkedList
, and HashMap
.
Yes, exercises involve creating and manipulating various types of collections to understand their usage.
Yes, it’s designed for beginners with clear explanations and step-by-step examples.
A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler will suffice.
Yes, it covers how collections offer efficient ways to store, retrieve, and process groups of objects.
It typically takes a few hours to complete, depending on your learning pace.
Yes, this Java Collections training is completely free on Iqra Technology Academy’s website.