Lesson 7 / 11

Inheritance, Interfaces and Polymorphism

Inheritance

public class CertifiedCourse extends Course {
    private String certificateName;

    public CertifiedCourse(String title, int lessonCount, String certificateName) {
        super(title, lessonCount);
        this.certificateName = certificateName;
    }
}

extends gives CertifiedCourse everything Course already has — every field and method — plus its own additional field. super(...) calls the parent class’s constructor, so the shared setup logic in Course doesn’t need to be duplicated.

Interfaces

public interface Playable {
    void play();
}

public class VideoLesson implements Playable {
    public void play() {
        System.out.println("Playing video...");
    }
}

An interface defines a contract — a list of method signatures with no implementation. Any class that implements an interface must provide real implementations for all of them, or the code won’t compile. This is how completely unrelated classes (say, VideoLesson and AudioLesson) can still be treated identically by other code, as long as they both implement the same interface.

Polymorphism

Course course = new CertifiedCourse("Java", 11, "Java Pro");
System.out.println(course.getTitle());   // works, even though 'course' is declared as type Course

A variable declared as type Course can hold a CertifiedCourse object and still call any method Course defines — the actual runtime object decides which specific version of an overridden method actually runs. This is polymorphism, and it’s why you can write code that works with the general Course type while still supporting any number of more specific subclasses without modification.

abstract classes

public abstract class Notifier {
    public abstract void send(String message);

    public void announce(String message) {
        send("[Tutoline] " + message);
    }
}

An abstract class sits between a regular class and an interface — it can have real implemented methods (like announce) alongside methods that subclasses are required to implement (send), and it can never be instantiated directly with new.