Java · Lesson 7 of 9
Inheritance, interfaces & polymorphism
extends, super, @Override, abstract classes and interfaces.
- Inheritance:
class Developer extends Employee— the child reuses and extends the parent's fields and methods. Java allows one parent class. super(...)calls the parent's constructor;@Overridemarks a method that replaces the parent's version.- An abstract class can't be instantiated and may contain abstract methods that subclasses must implement.
- An interface is a contract of methods. A class can implement many interfaces:
class Intern implements Payable, Reviewable. - Polymorphism: a variable of a parent/interface type can hold any subtype, and the right overridden method runs at run time.
Prefer composition and small interfaces over deep inheritance chains.
Example program
Run with JDK 17+: java Main.javaimport java.util.List;
public class Main {
interface Payable {
double monthlyPay();
}
static abstract class Employee implements Payable {
protected final String name;
Employee(String name) { this.name = name; }
abstract String role();
public String toString() { return name + " — " + role() + " — ₹" + Math.round(monthlyPay()); }
}
static class Developer extends Employee {
private final double lpa;
Developer(String name, double lpa) { super(name); this.lpa = lpa; }
String role() { return "Developer"; }
public double monthlyPay() { return lpa * 100000 / 12; }
}
static class Intern extends Employee {
private final double stipend;
Intern(String name, double stipend) { super(name); this.stipend = stipend; }
String role() { return "Intern"; }
public double monthlyPay() { return stipend; }
}
public static void main(String[] args) {
List<Payable> team = List.of(new Developer("Arjun", 8.4), new Intern("Diya", 15000));
double total = 0;
for (Payable p : team) {
System.out.println(p); // polymorphic toString / monthlyPay
total += p.monthlyPay();
}
System.out.println("Payroll: ₹" + Math.round(total));
}
}
Output
Arjun — Developer — ₹70000
Diya — Intern — ₹15000
Payroll: ₹85000
Save the code as Main.java and run java Main.java (JDK 11+ runs single files directly). Names and figures are fictional.
Key points
- extends = inherit from one class.
- implements = fulfil one or more interfaces.
- Polymorphism: parent type, child behaviour.
Quick check
How many classes can a Java class extend directly?
Java supports single class inheritance; multiple types are achieved with interfaces.
Practice task
Add a Manager subclass whose monthly pay is a fixed salary plus a 10% bonus, and include it in the team list.
Interview corner
Abstract class vs interface?
An abstract class can hold state (fields), constructors and partial implementation; a class can extend only one. Interfaces define a contract (with optional default methods) and a class can implement many.
What is runtime polymorphism?
The JVM decides which overridden method to run based on the actual object type, not the variable type — dynamic dispatch.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.