Description:
Demonstrate inheritance with Vehicle and Car.
Demonstrate inheritance with Vehicle and Car.
Java Code
class Vehicle {
String brand = "Generic";
void honk() { System.out.println("Beep!"); }
}
class Car extends Vehicle {
int speed;
Car(String brand, int speed) {
this.brand = brand; this.speed = speed;
}
void info() { System.out.println(brand + " - " + speed + " km/h"); }
}
public class InheritanceDemo {
public static void main(String[] args) {
Car c = new Car("Toyota", 120);
c.honk(); c.info();
}
}
Expected Output
Beep!
Toyota - 120 km/h