Java: HandyPerson Example

UML diagram

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
// Purpose: Set up some classes and interfaces for further questions.
//          Style compromises were made to get this on one page.
// Author : Fred Swartz, 22 Feb 2006

////////////////////////////////////////////////////////////////////////// Human
public class Human {
    private String name;
    
    public Human(String name) { this.name = name; }
    public String getName()   { return name; }
    @Override public String toString() { return "Human " + getName(); }
}

//////////////////////////////////////////////////////////////////// Electrician
interface Electrician { public String changeBulb(); }

//////////////////////////////////////////////////////////////////////// Plumber
interface Plumber     { public String unplugDrain(); }

//////////////////////////////////////////////////////////////////// HandyPerson
class HandyPerson extends Human implements Electrician, Plumber {
    public        HandyPerson(String name) { super(name); }
    public String changeBulb() { return "HP: Unscrew old, screw in new";}
    public String unplugDrain(){ return "HP: Use plunger"; }
    @Override public String toString() { return "HandyPerson " + getName(); }
}

////////////////////////////////////////////////////////////////// NobodySpecial
class NobodySpecial extends Human {
    public      NobodySpecial(String name) { super(name); }
    public void changeBulb() {System.out.print("NS: Unscrew old, screw in new");}
    public void unplugDrain() {System.out.println("NS: Use plunger"); }
    @Override public String toString() { return "NobodySpecial " + getName(); }
}

////////////////////////////////////////////////////////////// IndustrialPlumber
class IndustrialPlumber extends Human implements Plumber {
    public        IndustrialPlumber(String name) { super(name); }
    public String unplugDrain() {return "IP: We'll add it to our list.";}
}

Exercises

  1. HandyPerson Interface Exercises - 1