-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultilevel_Inheritance_Demo.java
More file actions
82 lines (71 loc) · 2.49 KB
/
Multilevel_Inheritance_Demo.java
File metadata and controls
82 lines (71 loc) · 2.49 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Program: Multilevel Inheritance Demo
* Description: A Java program to demonstrate multilevel inheritance.
* - Level 1 (GrandParent): Accepts input.
* - Level 2 (Parent): Calculates logic (Multiplication Table).
* - Level 3 (Child): Displays output.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.util.Scanner;
// Level 1: Base Class
class GrandParent {
/**
* Method to accept integer input from the user.
* @return The entered integer.
*/
int acceptInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a number: ");
int no = scanner.nextInt();
return no;
}
}
// Level 2: Derived from GrandParent
class ParentCalculator extends GrandParent {
int index = 0;
int[] tableArray = new int[10];
/**
* Method to generate multiplication table and store in an array.
* @param num The number for which table is generated.
* @return Array containing the multiplication table.
*/
int[] generateTable(int num) {
index = 0; // Reset index
for (int i = 1; i <= 10; i++) {
tableArray[index] = num * i;
index++;
}
return tableArray;
}
}
// Level 3: Derived from ParentCalculator
class ChildDisplay extends ParentCalculator {
/**
* Method to display the elements of an integer array.
* @param arr The array to display.
*/
void displayTable(int[] arr) {
System.out.println("The table of entered number is: ");
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
}
// Main class to test Multilevel Inheritance
public class Multilevel_Inheritance_Demo {
public static void main(String[] args) {
System.out.println("---------------------------------------------");
System.out.println(" Multilevel Inheritance Demo");
System.out.println("---------------------------------------------");
// Creating an object of the most derived class (Level 3)
ChildDisplay obj = new ChildDisplay();
// Level 1 functionality: Accept Input
int num = obj.acceptInput();
// Level 2 functionality: Calculate Table
int[] result = obj.generateTable(num);
// Level 3 functionality: Display Result
obj.displayTable(result);
System.out.println("---------------------------------------------");
}
}