Object-Oriented Programming Lesson 18 of 21
Java Inheritance, Polymorphism and Abstraction
Build classes on top of others with extends and super, override methods, treat different objects the same way with polymorphism, and use abstract classes and interfaces.
Many things share common features. Students and teachers are both people; cars and jeepneys are both vehicles. Inheritance lets one class reuse and extend another, and polymorphism lets you treat related objects the same way. Together with encapsulation and abstraction, these are the four pillars of OOP.
Inheritance with extends
class Person {
protected String name;
Person(String name) {
this.name = name;
}
void introduce() {
System.out.println("Hi, I'm " + name + ".");
}
}
class Student extends Person {
private int gradeLevel;
Student(String name, int gradeLevel) {
super(name); // run Person's constructor
this.gradeLevel = gradeLevel;
}
void study() {
System.out.println(name + " is studying for the quarterly exam.");
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Ana", 11);
s.introduce(); // inherited from Person
s.study(); // defined in Student
}
}
Output
Hi, I'm Ana. Ana is studying for the quarterly exam.
Student extends Person: Student is the subclass (child); Person is the superclass (parent).- Student automatically has
nameandintroduce(). super(name)calls the parent's constructor. It must be the first line of the child's constructor.protectedlets subclasses use the field while still hiding it from unrelated classes.
Java allows only one parent class per class (single inheritance).
Overriding methods
A subclass can replace an inherited method with its own version:
class Person {
protected String name;
Person(String name) { this.name = name; }
void introduce() { System.out.println("Hi, I'm " + name + "."); }
}
class Teacher extends Person {
private String subject;
Teacher(String name, String subject) {
super(name);
this.subject = subject;
}
@Override
void introduce() {
super.introduce(); // reuse the parent's version
System.out.println("I teach " + subject + ".");
}
}
public class Main {
public static void main(String[] args) {
new Teacher("Sir Reyes", "ICT").introduce();
}
}
Output
Hi, I'm Sir Reyes. I teach ICT.
@Override asks the compiler to check that you're really overriding a parent method. If you misspell the name, you get an error instead of a silent bug.
Polymorphism
"Many forms": a variable of the parent type can hold any child object, and calling a method runs the child's version:
class Vehicle {
double fare(double km) { return 0; }
String type() { return "Vehicle"; }
}
class Jeepney extends Vehicle {
@Override double fare(double km) { return 13 + Math.max(0, km - 4) * 1.80; }
@Override String type() { return "Jeepney"; }
}
class Tricycle extends Vehicle {
@Override double fare(double km) { return 20 + km * 5; }
@Override String type() { return "Tricycle"; }
}
class Taxi extends Vehicle {
@Override double fare(double km) { return 50 + km * 13.5; }
@Override String type() { return "Taxi"; }
}
public class Main {
public static void main(String[] args) {
Vehicle[] rides = { new Jeepney(), new Tricycle(), new Taxi() };
double km = 6;
for (Vehicle v : rides) {
System.out.printf("%-9s ₱%.2f%n", v.type(), v.fare(km));
}
}
}
Output
Jeepney ₱16.60 Tricycle ₱50.00 Taxi ₱131.00
The loop doesn't know or care which kind of vehicle each one is. It just calls fare(), and each object answers in its own way. Adding a new vehicle type later requires no change to the loop. (These fares are made-up examples.)
Abstract classes
Vehicle.fare() returning 0 makes no sense; every real vehicle should define its own fare. Mark the class and method abstract:
abstract class Shape {
abstract double area(); // no body: subclasses MUST provide it
void describe() {
System.out.printf("%s with area %.2f%n", getClass().getSimpleName(), area());
}
}
class Circle extends Shape {
private double r;
Circle(double r) { this.r = r; }
double area() { return Math.PI * r * r; }
}
class Rectangle extends Shape {
private double w, h;
Rectangle(double w, double h) { this.w = w; this.h = h; }
double area() { return w * h; }
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Rectangle(3, 4) };
for (Shape s : shapes) s.describe();
// new Shape(); // error: Shape is abstract; cannot be instantiated
}
}
Output
Circle with area 12.57 Rectangle with area 12.00
Interfaces
An interface is a list of methods a class promises to provide. A class can implement many interfaces:
interface Payable {
double amountDue();
}
class Tuition implements Payable {
public double amountDue() { return 15000; }
}
class CanteenBill implements Payable {
private int meals;
CanteenBill(int meals) { this.meals = meals; }
public double amountDue() { return meals * 45; }
}
public class Main {
public static void main(String[] args) {
Payable[] bills = { new Tuition(), new CanteenBill(20) };
double total = 0;
for (Payable p : bills) total += p.amountDue();
System.out.println("Total due: ₱" + total);
}
}
Output
Total due: ₱15900.0
The four pillars of OOP
| Pillar | Meaning | Java tools |
|---|---|---|
| Encapsulation | Hide data, expose safe methods | private, getters/setters |
| Inheritance | Reuse and extend a class | extends, super |
| Polymorphism | One interface, many behaviors | overriding, parent-type variables |
| Abstraction | Show what, hide how | abstract, interface |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04