Skip to content

Commit 9638748

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: Somesh diwan <[email protected]>
1 parent 1234783 commit 9638748

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed
Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,47 @@
1-
constructors. In Java, constructors are special methods used to initialize objects of a class.
1+
In Java, constructors are special methods used to initialize objects of a class.
22

33
They have the same name as the class and do not have a return type.
44

55
If you were thinking about adding parameters to the class constructor, here's how you would do it:
66

77
What is a Constructor?
8-
98
A constructor is used to initialize the state of an object when it's created. It can take parameters,
109
and you can use these parameters to initialize the object's fields.
1110

1211
Syntax for Constructor:
13-
1412
If you want to create a constructor that takes parameters (`int a, int b`), you would do it like this:
1513

1614
class Arithmetic {
1715
// Constructor that takes two integers
18-
public Arithmetic(int a, int b)
19-
{
16+
public Arithmetic(int a, int b) {
2017
// You could use 'a' and 'b' to initialize the object
2118
System.out.println("Constructor called with values: " + a + " and " + b);
2219
}
2320

2421
// Method to add two integers and return the sum
25-
public int add(int a, int b)
26-
{
22+
public int add(int a, int b) {
2723
return a + b;
2824
}
2925
}
3026

3127
When do you use a constructor?
32-
33-
- A constructor is automatically called when an object is created.
34-
28+
- A constructor is automatically called when an object is created.
3529
- If you want to pass parameters to initialize an object, you can define a **parameterized constructor**.
3630

3731
Example with Constructor:
38-
3932
Here’s how it would look in your case with `Arithmetic` class having a constructor that takes parameters:
4033

4134
class Arithmetic {
4235
// Constructor with parameters a and b
4336
public Arithmetic(int a, int b) {
4437
System.out.println("Arithmetic object created with values: " + a + " and " + b);
4538
}
46-
4739
// Method to add two integers and return their sum
48-
4940
public int add(int a, int b) {
5041
return a + b;
5142
}
5243
}
5344

54-
5545
class Adder extends Arithmetic {
5646
// Constructor of the subclass Adder
5747
public Adder(int a, int b) {
@@ -60,11 +50,8 @@ class Adder extends Arithmetic {
6050
}
6151
}
6252

63-
64-
public class Solution
65-
{
66-
public static void main(String[] args)
67-
{
53+
public class Solution {
54+
public static void main(String[] args) {
6855
// Create an Adder object, passing parameters for the constructor
6956
Adder a = new Adder(10, 20);
7057

@@ -76,31 +63,22 @@ public class Solution
7663
}
7764
}
7865

79-
8066
Breakdown:
81-
8267
1. Arithmetic Class:
83-
8468
- A constructor is defined that takes two parameters (`int a` and `int b`). It can be used to initialize the object or print something.
8569

8670
2. Adder Class:
87-
8871
- The constructor of `Adder` calls the `Arithmetic` class constructor using `super(a, b)` to initialize the base class.
8972

9073
3. Solution Class:
91-
92-
- An `Adder` object is created with parameters `10` and `20`. This triggers the constructor in `Arithmetic` and prints the initialization message.
74+
- An `dder` object is created with parameters `10` and `20`. This triggers the constructor in `Arithmetic` and prints the initialization message.
9375

9476
Output:
95-
9677
Arithmetic object created with values: 10 and 20
97-
9878
My superclass is: Arithmetic
99-
10079
42 13 20
10180

10281
Key Points about Constructors:
103-
10482
- A constructor does not have a return type.
10583
- A constructor with parameters is called a parameterized constructor.
106-
- In inheritance, you can call the superclass constructor using `super()` in the subclass constructor.
84+
- In inheritance, you can call the superclass constructor using `super()` in the subclass constructor.

0 commit comments

Comments
 (0)