Core Java & Object-Oriented Architecture
22 mins read
Free Lesson
Master Encapsulation, Constructors, Inheritance with extends, and Method Overriding with @Override.
The 4 Pillars of OOP in Java
// Base Superclass
public class Employee {
private String name;
protected double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public double calculateBonus() {
return this.salary * 0.10;
}
}
// Subclass with Inheritance
public class Manager extends Employee {
public Manager(String name, double salary) {
super(name, salary);
}
@Override
public double calculateBonus() {
return this.salary * 0.25; // Polymorphic override
}
}