Java Programming Lesson 9 | Classes, Objects & Constructors — the first full OOP lesson in Java
Java Programming Lesson 9
✅ We are entering Lesson-9: Classes, Objects & Constructors — the first full OOP lesson in Java. This is very important as it forms the foundation of all advanced Java programming.
📘 Lesson-9: Classes, Objects & Constructors
A class is a blueprint for creating objects (real-world entities).
Syntax:
Example:
✅ 2️⃣ What is an Object?
An object is an instance of a class.
Example:
✅ 3️⃣ Constructor
A constructor is a special method used to initialize objects.
-
Name = class name
-
No return type
Example:
✅ 4️⃣ Key Points About Objects & Classes
| Feature | Description |
|---|---|
| Class | Blueprint/Template |
| Object | Instance of Class |
| Constructor | Initializes Objects |
| Method | Defines Behavior |
| Attribute | Defines State/Property |
✅ 5️⃣ Example: Student Class
✅ 6️⃣ Default Constructor vs Parameterized Constructor
✅ 7️⃣ This Keyword
-
Refers to the current object
-
Useful when variable names shadow parameter names
✅ 8️⃣ Practice Tasks
1️⃣ Create a Book class:
- Attributes: title, author, price
- Constructor: initialize attributes
- Method: display info
- Create 2 books and display info
2️⃣ Create BankAccount class:
- Attributes: accountNumber, balance
- Methods: deposit(), withdraw(), displayBalance()
- Test with 2 accounts
3️⃣ Create Rectangle class:
- Attributes: length, width
- Method: area(), perimeter()
- Initialize rectangles and display results
Exception Handling (try, catch, finally)
✅ What is an Exception?
An exception is a problem that occurs during program execution.
Examples:
- Dividing by zero
- Accessing invalid array index
- File not found
If not handled, program crashes.
✅ Java Exception Handling Keywords
| Keyword | Purpose |
|---|---|
try | Code block to test for exceptions |
catch | Handle exception if occurs |
finally | Executes always (optional) |
throw | Manually throw exception |
throws | Declare exception in method |
✅ Basic Syntax
✅ Example 1 — Divide by Zero
✅ Output:
✅ Example 2 — Array Index Out of Bounds
✅ Multiple Catch Blocks
✅ Why Use Exception Handling?
✔ Prevent program crash
✔ Show friendly error message
✔ Debugging easier
✔ Required in professional apps
📝 Exercise for You ✅
Create a program:
1️⃣ Take two integers from user
2️⃣ Divide them → Handle division by zero
3️⃣ Access an array element → Handle invalid index
4️⃣ Print finally message
Example Output:
Reply with:
✅ Code
✅ Output
I will check & grade it ✅🔥
🎯 Quick Quiz
1️⃣ Which block always executes?
A) try
B) catch
C) finally
2️⃣ Which exception occurs for invalid array index?
A) ArithmeticException
B) ArrayIndexOutOfBoundsException
C) NullPointerException
Reply like:
1 → ?, 2 → ?
✅ Lesson-9 Completed!
Frequently Asked Questions-(FAQ)
What are classes in Java?
A class in Java is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods) that the created objects will have.
What are objects in Java?
An object is an instance of a class. It represents a real-world entity and allows programmers to access the data and methods defined inside a class.
Why do we use classes and objects in Java?
Classes and objects help organize code, improve reusability, simplify debugging, and support the core principles of object-oriented programming.
What is a constructor in Java?
A constructor is a special method that initializes an object when it is created. It has the same name as the class and does not have a return type.
What is the difference between a method and a constructor?
Methods perform actions or operations, while constructors initialize new objects. Constructors run automatically during object creation.
Why do we need constructors in Java?
Constructors ensure that an object begins its lifecycle with valid initial values. They help create fully initialized objects ready for use.
What is a default constructor?
A default constructor is automatically created by Java if no constructor is defined. It initializes object fields with default values like 0, null, or false.
What is a parameterized constructor?
Parameterized constructors accept arguments, allowing programmers to set specific values for object attributes at the time of creation.
Can a class have multiple constructors in Java?
Yes. A class can have multiple constructors using constructor overloading, where each constructor has different sets of parameters.
What is constructor overloading in Java?
Constructor overloading means creating multiple constructors with the same name but different parameters. This provides flexibility when creating objects with different initial values.
What is the 'this' keyword in Java constructors?
The this keyword refers to the current object. It is used to access instance variables and call other constructors within the same class.
How do objects store data in Java?
Objects store data in instance variables defined inside their class. Each object has its own independent copy of these variables.
Is it possible to create objects without using the new keyword?
Yes, but only in special cases like using factory methods, cloning, deserialization, or reflection. The most common way is using the new keyword.
How do classes support OOP concepts in Java?
Classes support key OOP principles like encapsulation, inheritance, abstraction, and polymorphism, making programs modular and scalable.
What is an instance variable?
An instance variable is a variable declared inside a class but outside methods. It stores data for each object individually.


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