Inheritance (Subclass and Superclass)
Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass).Keywords:
extends: inherits from a classimplements: inherits from an interface
class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}class Car extends Vehicle {
private String modelName = "Mustang";
public static void main(String[] args) {
Car myCar = new Car();
myCar.honk();
System.out.println(myCar.brand + " " + myCar.modelName);
}
}