-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSingle_Inheritance_Demo.java
More file actions
60 lines (52 loc) · 2.08 KB
/
Single_Inheritance_Demo.java
File metadata and controls
60 lines (52 loc) · 2.08 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
/**
* Program: Single Inheritance Demo
* Description: A Java program to demonstrate single-level inheritance.
* - Parent class: Contains logic to generate and print a multiplication table.
* - Child class: Inherits from Parent and adds functionality to accept user input.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.util.Scanner;
// Base class (Parent)
class ParentOps {
/**
* Method to print a multiplication table for a given number.
* @param num The number for which the table is generated.
*/
void printTable(int num) {
System.out.println("Multiplication Table for " + num + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(num + " X " + i + " = " + (num * i));
}
}
}
// Derived class (Child) inheriting from ParentOps
class ChildOps extends ParentOps {
/**
* Method to accept an integer input from the user.
* @return The integer entered by the user.
*/
int acceptInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a number: ");
int no = scanner.nextInt();
// Note: Scanner is not closed here to avoid closing System.in if used elsewhere,
// but in a real-world app, resource management is key.
return no;
}
}
// Main class to test Single Inheritance
public class Single_Inheritance_Demo {
public static void main(String[] args) {
System.out.println("---------------------------------------------");
System.out.println(" Single Inheritance Demo");
System.out.println("---------------------------------------------");
// Creating an object of the Child class
ChildOps childObj = new ChildOps();
// Using method from Child class to input number
int number = childObj.acceptInput();
// Using inherited method from Parent class to print table
childObj.printTable(number);
System.out.println("---------------------------------------------");
}
}