Inheritance • If we wanted to create a new class which was closely based on an existing class, save for a few changes, we can easily accomplish this with subclasses • The nature of classes allow us to create subclasses which inherit all the properties of the superclass (along with any additional attributes/methods specific to the subclass). • This OOP feature is called inheritance and is extremely useful when creating large programs with many objects Inheritance • Consider the class Vehicle below: public class Vehicle{ int engineSize; String make; int weight; void Vehicle(int e, String m, int w){ // constructor } • I would like to create a subclass called Car which inherits all the properties of Vehicle. Inheritance • We can accomplish this using the extends keyword: public class Car extends Vehicle{ } • Without further specification, the class Car has already inherited all the properties of Vehicle • Any new attributes/methods of Car will be unique only to Car (and not visible to Vehicle) Inheritance • Recall that the constructor accepts as its parameters some values and sets the attributes of the object to those values • Creating a constructor method for the subclass requires us to: 1. Invoke the constructor from the superclass to set all inherited attributes 2. Set all non-inherited values in the subclass. public class Car extends Vehicle{ private String owner; super public void Car(int e, String m, int w, String o ) { keyword super(e, s, w); invokes the this.owner = o; constructor from the } super class