Building Java Programs

Lab: Inheritance

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

Some extensions by T W Bennet

Lab goals

Goals for this problem set:

Inheritance (syntax)

public class ClassName extends SuperClass {
    ...
}
		
super.methodName(parameters);
		

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 : MonsterTruck practice-it

MonsterTruck bigfoot = new MonsterTruck();
bigfoot.m1();                  // monster 1
bigfoot.m2();                  // truck 1 / car 1
System.out.println(bigfoot);   // monster vroomvroom

Employee class hierarchy

Exercise : Marketer

Exercise : Janitor

Exercise : HarvardLawyer

Exercise : Employee Meeting

Write a small Java program in which you

Employee Meeting

The output from your program should look something like this:

Taking dictation of text: blah blah blah
Hours: 40 Paid: 40000.0 Vac: 10 days on yellow form.

I'll see you in court!
Hours: 40 Paid: 40000.0 Vac: 15 days on pink form.

I could file all day!
Hours: 40 Paid: 45000.0 Vac: 10 days on yellow form.

Workin' for the man.
Hours: 80 Paid: 30000.0 Vac: 5 days on yellow form.

I'll see you in court!
Hours: 40 Paid: 48000.0 Vac: 18 days on pinkpinkpinkpink form.

Act now, while supplies last!
Hours: 40 Paid: 50000.0 Vac: 10 days on yellow form.