Java: OOP Basics
What you will learn
Inheritance extends a class, polymorphism lets you treat subclasses as their parent type, and annotations like @Override.
public class Animal {
public void speak() {
System.out.println("...");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof!");
}
}
extends creates an is-a relationship: a Dog is an Animal. The subclass inherits all non-private members.
Polymorphism
Animal myPet = new Dog();
myPet.speak(); // "Woof!" -- the Dog version runs
The JVM decides at runtime which method to call (dynamic dispatch).
Quick check below!