Learn Apex
Polymorphism
A variable typed as a parent can hold a child instance. Virtual methods dispatch to the runtime type — the basis for extensible handlers.
virtual

In Apex, use virtual / abstract on the parent,override on the child.

Runtime type drives sound()

Animal a = new Dog();

Woof!

Method resolved on the real instance type.

Apex-shaped
public virtual class Animal {
    public virtual String speak() {
        return '…';
    }
}
public class Dog extends Animal {
    public override String speak() {
        return 'Woof';
    }
}