Commit 4e63eb7
committed
feat: Add ParentDemo and ChildDemo with parameterized constructors and super calls
WHAT the code does:
Defines ParentDemo with two constructors:
- No-argument constructor prints "Non-Param of parent".
- Parameterized constructor prints "Param of parent <x>".
Defines ChildDemo extending ParentDemo with three constructors:
- No-argument constructor prints "Non-Param of child".
- Single-argument constructor prints "Param of child".
- Two-argument constructor calls super(x) then prints "2 param of child <y>".
Defines ParametrisedConstructors with main():
- Creates ChildDemo object using two-argument constructor.
WHY this matters:
Demonstrates **constructor chaining** in inheritance:
- Child constructors can explicitly call parent constructors using super().
Reinforces the role of super for invoking parent behavior.
Shows how object construction flows from superclass → subclass.
HOW it works:
ChildDemo c = new ChildDemo(10, 20):
- Calls ChildDemo(int x,int y).
- Executes super(x) → ParentDemo(int x) prints "Param of parent 10".
- Then prints "2 param of child 20".
If no super(...) is explicitly called, Java automatically inserts a no-argument super().
Tips and gotchas:
Always call super() explicitly when parent doesn’t have a no-argument constructor.
@ comments clarify that super refers to the immediate superclass.
Typo in comment: "supper is a keyword" → should be "super is a keyword".
Class and method names follow Java conventions, though ParametrisedConstructors could be renamed ConstructorChainingDemo for clarity.
Use-cases:
Educational demonstration of parameterized constructors and constructor chaining.
Prepares for real-world use where subclasses must initialize parent state.
Foundation for understanding initialization order in OOP.
Short key: class-parentdemo childdemo constructor-chaining super.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 75a46a6 commit 4e63eb7
1 file changed
+16
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | | - | |
8 | | - | |
| 5 | + | |
| 6 | + | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 11 | + | |
| 12 | + | |
17 | 13 | | |
18 | 14 | | |
19 | | - | |
20 | | - | |
| 15 | + | |
| 16 | + | |
21 | 17 | | |
22 | 18 | | |
23 | | - | |
24 | | - | |
| 19 | + | |
| 20 | + | |
25 | 21 | | |
26 | | - | |
27 | | - | |
| 22 | + | |
| 23 | + | |
28 | 24 | | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
36 | 31 | | |
37 | | - | |
| 32 | + | |
38 | 33 | | |
39 | 34 | | |
0 commit comments