java / beginner
Snippet
Class Inheritance
Inheritance allows one class (child) to acquire the properties and methods of another class (parent). In Java, we use the 'extends' keyword.
snippet.java
1
2
3
4
5
6
7
class Vehicle {void honk() { System.out.println("Beep!"); }}class Car extends Vehicle {String model = "Mustang";}
Breakdown
1
class Vehicle { ... }
The parent class (superclass) with a general method.
2
class Car extends Vehicle { ... }
The child class (subclass) that inherits everything from Vehicle.
3
void honk() { ... }
A method defined in the parent class that the Car class can also use.