Commit d887f79
committed
feat: Add Parent and Child classes to demonstrate constructor chaining in inheritance
WHAT the code does:
Defines Parent with a no-argument constructor printing "Parent Constructor".
Defines Child extending Parent with a no-argument constructor printing "Child Constructor".
Defines ContructorInInheritnce with main():
- Instantiates a Child object.
- Output shows constructor call order.
WHY this matters:
Demonstrates constructor chaining in Java inheritance:
- When a subclass object is created, its superclass constructor is called first.
- Ensures proper initialization of the inheritance hierarchy.
Reinforces that if no explicit super(...) call is provided, Java inserts one automatically.
HOW it works:
Child c = new Child():
- Calls Parent() constructor first → prints "Parent Constructor".
- Then calls Child() constructor → prints "Child Constructor".
Demonstrates implicit invocation of superclass constructors.
Tips and gotchas:
If Parent did not have a no-argument constructor, Child would need to explicitly call super(...) with parameters.
Constructors are not inherited but are chained in the object creation process.
The class name ContructorInInheritnce contains a typo; should be ConstructorInInheritance.
Best practice is to initialize fields in constructors rather than only printing messages.
Use-cases:
Educational demo of constructor behavior in inheritance hierarchies.
Foundation for learning how initialization works in multi-level inheritance.
Practical stepping stone toward using super(...) in parameterized constructors.
Short key: class-parent-child constructor-chaining inheritance.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent ccb83e0 commit d887f79
1 file changed
+7
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | | - | |
9 | | - | |
10 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | | - | |
16 | 13 | | |
17 | | - | |
18 | | - | |
| 14 | + | |
19 | 15 | | |
20 | 16 | | |
21 | | - | |
| 17 | + | |
0 commit comments