-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstract_Class_Demo.java
More file actions
55 lines (44 loc) · 1.83 KB
/
Abstract_Class_Demo.java
File metadata and controls
55 lines (44 loc) · 1.83 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
/**
* Program: Abstract Class Demonstration
* Description: A Java program to demonstrate the concept of abstraction using abstract classes
* and abstract methods. It defines an abstract class 'Shape' and a concrete class 'Square'.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
// Abstract class definition
abstract class Shape {
int dimension = 10; // Instance variable
// Abstract method (does not have a body)
abstract void calculateArea();
// Concrete method (has a body)
void displayMessage() {
System.out.println("Display method from abstract class 'Shape'");
}
}
// Concrete class extending the abstract class
class Square extends Shape {
// Providing implementation for the abstract method
@Override
void calculateArea() {
// Using the inherited variable 'dimension'
int area = dimension * dimension; // Assuming dimension is side length
System.out.println("Calculating Area of Square...");
System.out.println("Side Length: " + dimension);
System.out.println("The area of the square is: " + area);
}
}
// Main class to test the implementation
public class Abstract_Class_Demo {
public static void main(String[] args) {
System.out.println("---------------------------------------------");
System.out.println(" Abstract Class Demo");
System.out.println("---------------------------------------------");
// Instantiating the concrete class
Square sq = new Square();
// Calling the implemented abstract method
sq.calculateArea();
// Calling the concrete method inherited from abstract class
sq.displayMessage();
System.out.println("---------------------------------------------");
}
}