Skip to content

Commit 3e5bd29

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

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Section10Methods/Methods 2.O/src/Constructors.txt renamed to Section10Methods/Methods 2.O/src/Constructor.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Breakdown:
7171
- The constructor of `Adder` calls the `Arithmetic` class constructor using `super(a, b)` to initialize the base class.
7272

7373
3. Solution Class:
74-
- An `dder` object is created with parameters `10` and `20`. This triggers the constructor in `Arithmetic` and prints the initialization message.
74+
- An `adder` object is created with parameters `10` and `20`. This triggers the constructor in `Arithmetic` and prints the initialization message.
7575

7676
Output:
7777
Arithmetic object created with values: 10 and 20

0 commit comments

Comments
 (0)