Skip to content

Commit 8aeaf36

Browse files
committed
feat: Demonstrate constructor chaining with super() and overloaded constructors
WHAT the code does: - Defines class A with two constructors: - Default constructor → prints "in A". - Parameterized constructor (int n) → calls super() (Object’s constructor implicitly) and prints "in A int". - Defines class B extending A: - Default constructor → implicitly calls A’s default constructor, then prints "in B". - Parameterized constructor (int m) → explicitly calls super(m) to invoke A’s parameterized constructor, then prints "in B in int". - In main(): - A obj2 = new A(9); → runs A’s parameterized constructor, prints "in A int". - B obj1 = new B(5); → calls A(int) via super(m), prints "in A int", then prints "in B in int". WHY this matters: - Demonstrates **constructor chaining**: - Every constructor in a subclass must call a constructor of the parent class (either implicitly or explicitly). - `super()` → calls parent constructor. - `this()` → can call another constructor in the same class. - Highlights difference between: - Default constructor chaining → A() then B(). - Parameterized constructor chaining → A(int) then B(int). - Shows the order: parent constructor always runs before child constructor. HOW it works: 1. new A(9) → calls A(int) → prints "in A int". 2. new B(5) → calls B(int). - B(int) explicitly calls super(m) → invokes A(int) → prints "in A int". - Then B(int) continues → prints "in B in int". Tips and gotchas: - If a parent class doesn’t have a no-argument constructor, subclass constructors must explicitly call one of the parent’s parameterized constructors. - Java inserts `super();` implicitly in constructors if no explicit call is present. - Constructor chaining is critical for proper initialization of inheritance hierarchies. Use-cases / analogies: - Parent handing down setup instructions before child’s setup can proceed. - Like building a house: foundation (parent) must be laid before adding walls and windows (child). Short key: java constructor-chaining super this inheritance initialization. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent fa8e4be commit 8aeaf36

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

Section16StaticFinal/src/ThisKeyword.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
class A
2-
{
3-
public A(){
1+
class A {
2+
public A() {
43
System.out.println("in A");
54
}
65

7-
public A(int n)
8-
{
6+
public A(int n) {
97
super();
108
System.out.println("in A int ");
119
}
1210
}
1311

14-
class B extends A
15-
{
16-
public B(){
12+
class B extends A {
13+
public B() {
1714
System.out.println("in B");
1815
}
1916

20-
public B(int m)
21-
{
17+
public B(int m) {
2218
super(m);
2319
System.out.println("in B in int");
2420
}
2521
}
2622

27-
public class ThisKeyword
28-
{
23+
public class ThisKeyword {
2924
public static void main(String[] args) {
3025
/* B obj = new B(); */
3126
//calling default constructor without passing a value parameter valur.

0 commit comments

Comments
 (0)