© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_26

26. Inheritance – Code Reuse

Ron Dai1 
(1)
Seattle, WA, USA
 

As one of the OOP principles, inheritance is designed to centralize the common functionality of many different objects. As a result of that, it reduces duplicated code in many classes.

Inheritance introduces two types of classes: “superclass” and “subclass.” The subclass inherits from the superclass. The superclass is the same thing as the “base class.” The subclass contains not only all the methods and the fields inherited from the superclass, but also other methods and fields defined by the subclass.

For example, we define a new class called Sedan that inherits from the Car class we created earlier. The Car class implements an interface called Auto. In the Sedan class, we define a Boolean field isFourDoorHatchback and a method called isFourWheelDrive() .

The keyword extends is used to describe the class that Sedan inherits from class Car .
public class Sedan extends Car {
       public Boolean isFourDoorHatchback;
       public Boolean isFourWheelDrive(){
              return true;
       }
}
We then create a main method in a Driver class to play with the Sedan class .
public class Driver {
       public static void main(String[] args) {
              Sedan sedan = new Sedan();
              sedan.start();
              sedan.stop();
              sedan.turn();
              sedan.back();
              sedan.park();
              sedan.setMaker("Toyota");
              sedan.getMaker();
              sedan.isFourDoorHatchback = true;
              sedan.isFourWheelDrive();
       }
}
As you see, the Sedan object (i.e., sedan) has all the methods and fields inherited from its superclass Car. In addition to that, Sedan class has its own method and field. In the same main method, we add more code:
              Car car = new Sedan();
              car.start();
              car.stop();
              car.turn();
              car.back();
              car.park();
              car.setMaker("Toyota");
              car.getMaker();

This example tells us that we can create an object from a superclass (i.e., Car) instantiated from its subclass (i.e., Sedan). All the methods and fields under its superclass are available as expected, but the methods and fields under its subclass are not accessible.

If we try to do:
              Sedan sedan2 = new Car();
We will get error message:
"Type mismatch: cannot convert from Car to Sedan".

This clearly tells us that we are not allowed to create a subclass object (i.e., Sedan) instantiated from its superclass (i.e., Car).

In Java, however, it doesn’t support multiple inheritance. Instead, it uses an interface to achieve the same goal as what multiple inheritance attempts to do in other programming languages.

Problems

  1. 1.
    Which of the following is the correct syntax to indicate that class A is a subclass of B?
    1. (a)

      public class A : super B {

       
    2. (b)

      public class B extends A {

       
    3. (c)

      public class A extends B {

       
    4. (d)

      public A(super B) {

       
    5. (e)

      public A implements B {

       
     
  2. 2.
    Consider the following classes:
    public class Vehicle {...}
    public class Car extends Vehicle {...}
    public class SUV extends Car {...}
    Which of the following are legal statements?
    1. (a)

      Car c = new Vehicle();

       
    2. (b)

      SUV s = new SUV();

       
    3. (c)

      SUV s = new Car();

       
    4. (d)

      Car c = new SUV();

       
    5. (e)

      Vehicle v = new Car();

       
    6. (f)

      Vehicle v = new SUV();

       
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.161.116