-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaInheritance2.java
More file actions
51 lines (40 loc) · 992 Bytes
/
Copy pathJavaInheritance2.java
File metadata and controls
51 lines (40 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Question:
Create a class Arithmetic with a method add()
that returns the sum of two integers.
Create another class Adder that inherits
from Arithmetic.
The add() method should return:
a + b
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
// Parent class
class Arithmetic {
// Return sum of two numbers
int add(int a, int b) {
return a + b;
}
}
// Child class inheriting Arithmetic
class Adder extends Arithmetic {
// No extra code needed
}
public class Solution {
public static void main(String[] args) {
// Create Adder object
Adder a = new Adder();
// Print superclass name
System.out.println("My superclass is: "
+ a.getClass().getSuperclass().getName());
// Call add method
System.out.print(
a.add(10, 32) + " " +
a.add(10, 3) + " " +
a.add(10, 10) + "\n"
);
}
}