Skip to content

Commit 0de1372

Browse files
committed
📘 docs: Explain difference between constructors and methods in Java
🔑 Short Key: constructor-vs-method 🧠 Logic & Highlights: - **Constructor**: special block to initialize objects, name matches class, no return type, invoked automatically with `new` - **Method**: defines object behavior, can have any name, must have a return type, invoked explicitly 📝 Key Differences: - Purpose: initialize object vs. perform actions - Name: same as class vs. any name - Return type: none vs. required - Invocation: automatic vs. explicit - Inheritance: not inherited vs. inherited/overridable - Overloading: supported for both 📊 Added Comparison Table: | Feature | Constructor | Method | |---------------|------------------------------------|---------------------------------------| | Purpose | Initialize object | Define behavior/operations | | Name | Same as class | Any valid identifier | | Return Type | None | Must return type (`int`, `void`, etc.)| | Invocation | Auto on object creation | Explicit call on object | | Inheritance | Not inherited | Inherited and overridable | | Overloading | Supported | Supported | 📘 Example: - Created a `Car` class with parameterized constructor to set `model` & `year` - Added `displayDetails()` method to show details - Demonstrated usage in `Main` class 🎯 Learning Outcomes: - Clear understanding of when and how to use constructors vs methods** - Demonstrated real-world example with `Car` class. Signed-off-by: Somesh diwan <[email protected]>
1 parent 1036eaf commit 0de1372

File tree

1 file changed

+1
-1
lines changed
  • Section11ObjectOrientedProgramming/OOPs 2.O/src/Test

1 file changed

+1
-1
lines changed

Section11ObjectOrientedProgramming/OOPs 2.O/src/Test/Animal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public void makeSound()
99
{
1010
System.out.println("MAke Some sound");
1111
}
12-
}
12+
}

0 commit comments

Comments
 (0)