Commit 9638748
committed
feat: Demonstrate parameterized constructors with inheritance in Java
🔑 Short Key: constructor-inheritance-super
🧠 Logic & Highlights:
- `Arithmetic` class → defines a parameterized constructor `(int a, int b)`
- **`Adder` class** → extends `Arithmetic` and uses `super(a, b)` to call the parent constructor
- When `new Adder(10, 20)` is created:
- `Arithmetic` constructor runs → prints initialization message
- Then object is fully created and usable
- `getSuperclass().getName()` → retrieves parent class name
- `add()` method works independently of constructor
📌 Output Walkthrough:
1. `Arithmetic object created with values: 10 and 20`
2. `My superclass is: Arithmetic`
3. `42 13 20` → results of `add()` method
🎯 Learning Outcomes:
- Constructors → special methods invoked at object creation, no return type
- Parameterized constructors → allow passing values during initialization
- Inheritance → subclass constructor must explicitly call `super(...)` if parent has no default constructor
- Shows constructor chaining and method usage together.
Signed-off-by: Somesh diwan <[email protected]>1 parent 1234783 commit 9638748
1 file changed
+8
-30
lines changedLines changed: 8 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
13 | | - | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | | - | |
19 | | - | |
| 16 | + | |
20 | 17 | | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
24 | 21 | | |
25 | | - | |
26 | | - | |
| 22 | + | |
27 | 23 | | |
28 | 24 | | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 28 | + | |
35 | 29 | | |
36 | 30 | | |
37 | 31 | | |
38 | | - | |
39 | 32 | | |
40 | 33 | | |
41 | 34 | | |
42 | 35 | | |
43 | 36 | | |
44 | 37 | | |
45 | 38 | | |
46 | | - | |
47 | 39 | | |
48 | | - | |
49 | 40 | | |
50 | 41 | | |
51 | 42 | | |
52 | 43 | | |
53 | 44 | | |
54 | | - | |
55 | 45 | | |
56 | 46 | | |
57 | 47 | | |
| |||
60 | 50 | | |
61 | 51 | | |
62 | 52 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 53 | + | |
| 54 | + | |
68 | 55 | | |
69 | 56 | | |
70 | 57 | | |
| |||
76 | 63 | | |
77 | 64 | | |
78 | 65 | | |
79 | | - | |
80 | 66 | | |
81 | | - | |
82 | 67 | | |
83 | | - | |
84 | 68 | | |
85 | 69 | | |
86 | 70 | | |
87 | | - | |
88 | 71 | | |
89 | 72 | | |
90 | 73 | | |
91 | | - | |
92 | | - | |
| 74 | + | |
93 | 75 | | |
94 | 76 | | |
95 | | - | |
96 | 77 | | |
97 | | - | |
98 | 78 | | |
99 | | - | |
100 | 79 | | |
101 | 80 | | |
102 | 81 | | |
103 | | - | |
104 | 82 | | |
105 | 83 | | |
106 | | - | |
| 84 | + | |
0 commit comments