Skip to content

Commit 1770d66

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 9638748 commit 1770d66

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

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

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
The difference between a constructor and a method in Java is fundamental in object-oriented programming. Let's break down the key differences:
1+
The difference between a constructor and a method in Java is fundamental in object-oriented programming.
2+
3+
Let's break down the key differences:
24

35
1. Purpose
46
- Constructor:
@@ -18,82 +20,65 @@ The difference between a constructor and a method in Java is fundamental in obje
1820
- For example, a method can be named `add`, `multiply`, or `display`.
1921

2022
3. Return Type
21-
2223
- Constructor:
23-
2424
- A constructor does not have a return type. It doesn't return anything, not even `void`.
2525

26-
- Example:
27-
26+
- Example:
2827
public Arithmetic(int a, int b) { ... }
2928

3029
- Method:
31-
3230
- A method must have a return type. It can return a value (e.g., `int`, `String`, `void`), or in some cases, it can return nothing (in which case it would have a return type of
3331
void).
3432

3533
- Example:
36-
3734
public int add(int a, int b) { ... }
3835

3936
4. Invocation
4037

4138
- Constructor:
42-
4339
- A constructor is automatically called when you **create an object** using the `new` keyword.
4440

4541
- Example:
46-
4742
Arithmetic obj = new Arithmetic(10, 20); // Constructor is called here
4843

4944
- Method:
50-
5145
- A method is called explicitly after the object has been created, using the object's reference.
5246

5347
- Example:
54-
5548
int sum = obj.add(10, 20); // Calling the add method
5649

5750
5. Parameters:
5851

5952
- Constructor:
60-
6153
- Constructors can take parameters, allowing you to pass values to initialize the object at the time of creation.
6254

6355
- Example:
64-
6556
public Arithmetic(int a, int b) {
6657
// Initializes the object
6758
}
6859

6960
- Method:
70-
7161
- Methods can also take parameters, but they are called after the object is created.
7262

7363
- Example:
74-
7564
public int add(int a, int b) {
7665
return a + b;
7766
}
7867

7968
6. Inheritance
80-
8169
- Constructor:
82-
83-
- Constructors are not inherited. Each class must define its own constructors, although the subclass can call the superclass constructor using `super()`.
70+
- Constructors are not inherited. Each class must define its own constructors,
71+
although the subclass can call the superclass constructor using `super()`.
8472

8573
- Method:
86-
8774
- Methods are inherited. A subclass can inherit methods from its superclass and can override them if needed.
8875

8976
7. Constructor Overloading vs Method Overloading
90-
9177
- Constructor:
92-
93-
- Constructor overloading is allowed, meaning you can have multiple constructors with different parameters in the same class.
78+
- Constructor overloading is allowed, meaning you can have multiple constructors with
79+
different parameters in the same class.
9480

9581
- Example:
96-
9782
public Arithmetic() {
9883
// Default constructor
9984
}
@@ -103,12 +88,10 @@ The difference between a constructor and a method in Java is fundamental in obje
10388
}
10489

10590
- Method:
106-
10791
- Method overloading is also allowed, which means you can have multiple methods with the same name but different parameter lists.
92+
10893
- Example:
109-
11094
public int add(int a, int b) { ... }
111-
11295
public int add(int a, int b, int c) { ... }
11396

11497
Summary of Differences:
@@ -152,11 +135,7 @@ public class Main {
152135
}
153136
}
154137

155-
156138
Output:
157-
158139
Model: Tesla Model 3, Year: 2021
159-
160140
- Constructor: `new Car("Tesla Model 3", 2021)` initializes the object with the `model` and `year`.
161-
162-
- Method: `displayDetails()` is called after the object is created to show the car's details.
141+
- Method: `displayDetails()` is called after the object is created to show the car's details.

0 commit comments

Comments
Β (0)