Skip to content

Commit 4e63eb7

Browse files
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

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
class ParentDemo
2-
{
3-
ParentDemo()
4-
{
1+
class ParentDemo {
2+
ParentDemo() {
53
System.out.println("Non-Param of parent");
64
}
7-
ParentDemo(int x)
8-
{
5+
6+
ParentDemo(int x) {
97
System.out.println("Param of parent "+x);
108
}
119
}
1210

13-
class ChildDemo extends ParentDemo
14-
{
15-
ChildDemo()
16-
{
11+
class ChildDemo extends ParentDemo {
12+
ChildDemo() {
1713
System.out.println("Non-Param of child");
1814
}
19-
ChildDemo(int y)
20-
{
15+
16+
ChildDemo(int y) {
2117
System.out.println("Param of child");
2218
}
23-
ChildDemo(int x,int y)
24-
{
19+
20+
ChildDemo(int x,int y) {
2521
super(x);
26-
//this will caalled the super class.
27-
// super is keyword which refer to the super class.
22+
// this will call the super class.
23+
// supper is a keyword that refers to the super class.
2824
System.out.println("2 param of child "+y);
2925

3026
}
3127
}
32-
public class ParametrisedConstructors
33-
{
34-
public static void main(String[] args)
35-
{
28+
29+
public class ParametrisedConstructors {
30+
public static void main(String[] args) {
3631
ChildDemo c=new ChildDemo(10,20);
37-
//we call parameterized constructer of super class.
32+
//we call parameterized constructor of super class.
3833
}
3934
}

0 commit comments

Comments
 (0)