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


Object-Oriented Programming, commonly known as OOP, is one of the most popular and powerful programming paradigms used in modern software development. It allows developers to structure code in a way that is clean, organized, reusable, and similar to real-world objects.

Whether you’re a beginner or an experienced developer, understanding classes and objects is the first step to mastering OOP.

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 → ?

Conclusion

Object-Oriented Programming (OOP) is more than just a coding style — it’s a powerful way of thinking about software. By understanding classes (the blueprint) and objects (the real instance), you can build complex, efficient, and scalable applications.

Once you master these basics, learning the other pillars of OOP becomes much easier.


✅ Lesson-11 Completed!



NEXT PART - Lesson-12: Inheritance & Polymorphism.


Frequently Asked Questions-(FAQ)

What is Object-Oriented Programming (OOP) in Java?

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure code. It focuses on reusability, modularity, and organizing code around real-world entities.

What is a class in Java?

A class is a blueprint or template that defines properties (variables) and behaviors (methods) for creating objects.

What is an object in Java?

An object is an instance of a class. It represents a real-world entity and contains its own data and methods.

How do you create an object in Java?

You use the new keyword to create an object, such as: Car myCar = new Car();

What are the main features of OOP?

The four major features of OOP in Java are: Encapsulation Inheritance Polymorphism Abstraction

Why is OOP important in Java?

OOP improves code organization, makes programs scalable, reduces redundancy, and allows developers to build complex, maintainable applications.

What is the difference between a class and an object?

A class is a blueprint, while an object is a real instance created from that blueprint.

Can a Java class exist without creating objects?

Yes, you can define a class without creating objects, but to use its non-static variables or methods, an object must be created.

What is a constructor in Java OOP?

A constructor is a special method used to initialize objects. It has the same name as the class and no return type.

Why do we use constructors in OOP?

Constructors help assign initial values to object properties, ensuring objects start in a valid state.

What is the default constructor?

If you do not write any constructor, Java automatically creates a default constructor with no parameters.

Can a class have multiple constructors?

Yes, using constructor overloading, a class can have multiple constructors with different parameters.

What is the role of the 'new' keyword?

The new keyword allocates memory in the heap and creates a fresh object instance.

What is encapsulation in OOP?

Encapsulation means wrapping data (variables) and methods into a single unit and restricting access with access modifiers like private.

What is the 'this' keyword in Java classes?

this refers to the current object. It helps avoid confusion between class variables and method parameters.

No comments

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

Theme images by 5ugarless. Powered by Blogger.