University of Cincinnati logo and link  
Inheritance in Java
 
  UC ingot 
  • Now that we know about classes and objects, it is time to move on to another part of object oriented programming - inheritance.
    • Inheritance allows you to re-use parts of another class, and expand on it or tailor parts to be more specific.
    • We use the extends keyword in Java.
    • This is also called an is-a relationship.
  • Think of the old Car example.
    • We could have a class Vehicle.
      • class Car, Moped, and Motorcyle could all extend Vehicle, because they are all Vehicles.
      • class Cavalier could extend class Car, because a Cavalier is a car.
      • Then, my Cavalier could be an object of Cavalier, let's call it myCavalier.
  • The child extends the parent, thus the phrase inheritance.
    • In Java, we call the parent the superclass and the child the subclass.
    • In our example, Vehicle would be a superclass, and Car a subclass to Vehicle.  But then Car would be a superclass to Cavalier, which would be a subclass.  So classes can both extend and be extended.
    • Generally, the more you subclass, the more detail the resulting subclasses have.