DMCA.com Protection Status Java Programming Lesson-11 | Object-Oriented Programming (OOP) — Classes & Objects - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson-11 | Object-Oriented Programming (OOP) — Classes & Objects

Java Programming Lesson-11

Now we dive into Lesson-11: Object-Oriented Programming (OOP) — Classes & Objects, the core of Java.

OOP is essential for writing professional, reusable, and organized code.


📘 Lesson-11 — Classes & Objects in Java

✅ What is a Class?

A class is like a blueprint for an object.
It defines attributes (variables) and behaviors (methods).

class Car { String color; String model; void drive() { System.out.println("Car is driving"); } }

✅ What is an Object?

An object is an instance of a class — like a real car built from blueprint.

Car myCar = new Car(); // Create object myCar.color = "Red"; myCar.model = "Toyota"; myCar.drive(); // Call method

✅ Output:

Car is driving

✅ Anatomy of a Class
PartMeaning
Fields / Attributes            Variables storing state (e.g., color, age)
Methods / Functions            Behavior (e.g., drive(), start())
Constructor            Special method to initialize object

✅ Constructor (Initialize Object)
class Car { String model; int year; // Constructor Car(String m, int y) { model = m; year = y; } void display() { System.out.println("Model: " + model + ", Year: " + year); } } public class Main { public static void main(String[] args) { Car car1 = new Car("Toyota", 2023); car1.display(); } }

✅ Output:

Model: Toyota, Year: 2023

✅ Accessing Object Members
class Student { String name; int age; void showInfo() { System.out.println("Name: " + name + ", Age: " + age); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.name = "Jitu"; s1.age = 21; s1.showInfo(); } }

✅ Key Points About Objects

✔ Each object has its own copy of attributes
✔ Methods can access object data
✔ Multiple objects can be created from same class


✅ Example: Multiple Objects
Student s1 = new Student(); s1.name = "Alice"; s1.age = 20; Student s2 = new Student(); s2.name = "Bob"; s2.age = 22; s1.showInfo(); // Name: Alice, Age: 20 s2.showInfo(); // Name: Bob, Age: 22

📝 Exercise for You ✅

Create a program:

1️⃣ Create class Book with attributes: title, author, price
2️⃣ Create a constructor to initialize values
3️⃣ Add method display() to print book info
4️⃣ Create 2 book objects and display their info

Example Output:

Title: Java Basics, Author: Jitu, Price: 500 Title: Advanced Java, Author: Hasan, Price: 750

Reply with:
✅ Your Code
✅ Output

I’ll check & give feedback 🔥


🎯 Quick Quiz

1️⃣ Which keyword creates an object?
A) class
B) new
C) object

2️⃣ What is a blueprint in Java OOP?
A) Object
B) Class
C) Method

Reply like:
1 → ?, 2 → ?


✅ Lesson-11 Completed!



NEXT PART - Lesson-12: Inheritance & Polymorphism.

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.