-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClasses.java
More file actions
82 lines (57 loc) · 2.04 KB
/
Copy pathAbstractClasses.java
File metadata and controls
82 lines (57 loc) · 2.04 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
package oop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* Abstract classes serve as templates that cannot be directly instantiated,
featuring a mix of abstract methods without bodies and concrete methods with implementations.
* They are designed to be subclassed, allowing for a combination of enforced structure and shared functionality.
*/
abstract class Employee {
Logger logger = LoggerFactory.getLogger(Employee.class);
protected String name;
protected double baseSalary;
Employee(String name, double baseSalary) {
this.name = name;
this.baseSalary = baseSalary;
}
// Abstract method: Every subclass MUST implement it
abstract double calculateSalary();
// Concrete method: Shared by all subclasses
void showInfo() {
logger.info("{} -> Salary: {}", name, calculateSalary());
}
}
// Concrete subclass that inherits from Employee
class FullTimeEmployee extends Employee {
// Constructor that calls the superclass constructor
FullTimeEmployee(String name, double baseSalary) {
super(name, baseSalary);
}
// Overriding the abstract method from the superclass
@Override
double calculateSalary() {
return baseSalary; // Fixed salary, no variations
}
}
// Concrete subclass that inherits from Employee
class CommissionEmployee extends Employee {
private final double commissions;
// Constructor that calls the superclass constructor and initializes commissions attribute
CommissionEmployee(String name, double baseSalary, double commissions) {
super(name, baseSalary);
this.commissions = commissions;
}
// Overriding the abstract method from the superclass
@Override
double calculateSalary() {
return baseSalary + commissions;
}
}
public class AbstractClasses {
public static void main(){
Employee e1 = new FullTimeEmployee("exampleName1", 1800);
Employee e2 = new CommissionEmployee("exampleName2", 1200, 650);
e1.showInfo();
e2.showInfo();
}
}