Skip to content

Commit 1036eaf

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 1770d66 commit 1036eaf

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

β€ŽSection10Methods/Methods 2.O/src/The difference between a constructor and a method in Java is fundamental in object-oriented programming.txtβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Let's break down the key differences:
88
- Its main role is to set up the initial state of the object, such as initializing fields or setting up resources.
99

1010
- Method:
11-
- A method is used to define behavior or operations that an object can perform. It can be called after the object is created to perform actions on the object.
11+
- A method is used to define behavior or operations that an object can perform.
12+
It can be called after the object is created to perform actions on the object.
1213

1314
2. Name
1415
- Constructor:

0 commit comments

Comments
Β (0)