University of Washington, AP/CS A

Lab 5: inheritance

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for this lab:

Exercise : Car and Truck practice-it

public class Car {
   public void m1() {
      System.out.println("car 1");
   }

   public void m2() {
      System.out.println("car 2");
   }

   public String toString() {
      return "vroom";
   }
}
public class Truck extends Car {
   public void m1() {
      System.out.println("truck 1");
   }
}
Truck mycar = new Truck();
System.out.println(mycar);    // vroom
mycar.m1();                   // truck 1
mycar.m2();                   // car 2

Exercise : Car and Truck revisited practice-it

public class Car {
   public void m1() {
      System.out.println("car 1");
   }

   public void m2() {
      System.out.println("car 2");
   }

   public String toString() {
      return "vroom";
   }
}
public class Truck extends Car {
   public void m1() {
      System.out.println("truck 1");
   }
    
   public void m2() {
      super.m1();
   }
    
   public String toString() {
      return super.toString() + super.toString();
   }
}
Truck mycar = new Truck();
System.out.println(mycar);    // vroomvroom
mycar.m1();                   // truck 1
mycar.m2();                   // car 1

Exercise : inheritance mystery practice-it

Assume the following classes have been defined:

public class A extends B {
    public void method2() {
        System.out.println("a 2");
    }
}
public class D extends B {
    public void method1() {
        System.out.println("d 1");
    }
}
public class C {
    public String toString() {
        return "c";
    }

    public void method1() {
        System.out.println("c 1");
    }

    public void method2() {
        System.out.println("c 2");
    }
}
public class B extends C {
    public String toString() {
        return "b";
    }

    public void method2() {
        System.out.println("b 2");
    }
}

continued on the next slide...

Exercise - inheritance mystery

b
c 1
a 2
b
c 1
b 2
c
c 1
c 2
b
d 1
b 2

Consider the code below that uses these classes.
Write each line of its output in the boxes at right.

C[] elements = {new A(),
                new B(),
                new C(),
                new D()};
for (int i = 0; i < elements.length; i++) {
    System.out.println(elements[i]);
    elements[i].method1();
    elements[i].method2();
}

Exercise : inheritance mystery 2 practice-it

Assume the following classes have been defined:

public class Denny extends John {
    public void method1() {
        System.out.print("denny 1 ");
    }
    public String toString() {
        return "denny " + super.toString();
    }
}
public class Cass {
    public void method1() {
        System.out.print("cass 1 ");
    }
    public void method2() {
        System.out.print("cass 2 ");
    }
    public String toString() {
        return "cass";
    }
}
public class Michelle extends John {
    public void method1() {
        System.out.print("michelle 1 ");
    }
}
public class John extends Cass {
    public void method2() {
        method1();
        System.out.print("john 2 ");
    }
    public String toString() {
        return "john";
    }
}

continued on the next slide...

Exercise - inheritance mystery 2

cass 1
cass 2
cass
denny 1
denny 1 john 2
denny john
cass 1
cass 1 john 2
john
michelle 1
michelle 1 john 2
john

Consider the code below that uses these classes.
Write each line of its output in the boxes at right.

Cass[] elements = {new Cass(),
                   new Denny(),
                   new John(),
                   new Michelle()};
for (int i = 0; i < elements.length; i++) {
    elements[i].method1();
    System.out.println();
    elements[i].method2();
    System.out.println();
    System.out.println(elements[i]);
    System.out.println();
}