Commit 1770d66
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- Section10Methods/Methods 2.O/src
1 file changed
+10
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | | - | |
22 | 23 | | |
23 | | - | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
| 26 | + | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | | - | |
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | | - | |
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
40 | 37 | | |
41 | 38 | | |
42 | | - | |
43 | 39 | | |
44 | 40 | | |
45 | 41 | | |
46 | | - | |
47 | 42 | | |
48 | 43 | | |
49 | 44 | | |
50 | | - | |
51 | 45 | | |
52 | 46 | | |
53 | 47 | | |
54 | | - | |
55 | 48 | | |
56 | 49 | | |
57 | 50 | | |
58 | 51 | | |
59 | 52 | | |
60 | | - | |
61 | 53 | | |
62 | 54 | | |
63 | 55 | | |
64 | | - | |
65 | 56 | | |
66 | 57 | | |
67 | 58 | | |
68 | 59 | | |
69 | 60 | | |
70 | | - | |
71 | 61 | | |
72 | 62 | | |
73 | 63 | | |
74 | | - | |
75 | 64 | | |
76 | 65 | | |
77 | 66 | | |
78 | 67 | | |
79 | 68 | | |
80 | | - | |
81 | 69 | | |
82 | | - | |
83 | | - | |
| 70 | + | |
| 71 | + | |
84 | 72 | | |
85 | 73 | | |
86 | | - | |
87 | 74 | | |
88 | 75 | | |
89 | 76 | | |
90 | | - | |
91 | 77 | | |
92 | | - | |
93 | | - | |
| 78 | + | |
| 79 | + | |
94 | 80 | | |
95 | 81 | | |
96 | | - | |
97 | 82 | | |
98 | 83 | | |
99 | 84 | | |
| |||
103 | 88 | | |
104 | 89 | | |
105 | 90 | | |
106 | | - | |
107 | 91 | | |
| 92 | + | |
108 | 93 | | |
109 | | - | |
110 | 94 | | |
111 | | - | |
112 | 95 | | |
113 | 96 | | |
114 | 97 | | |
| |||
152 | 135 | | |
153 | 136 | | |
154 | 137 | | |
155 | | - | |
156 | 138 | | |
157 | | - | |
158 | 139 | | |
159 | | - | |
160 | 140 | | |
161 | | - | |
162 | | - | |
| 141 | + | |
0 commit comments