Commit 3e5bd29
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: https://github.com/Someshdiwan <[email protected]>1 parent 4321ac4 commit 3e5bd29
1 file changed
+1
-1
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
0 commit comments