Peter W Schlamp

This is a program that demonstrates Java object oriented programming. Specifically, it shows the use of inheritance, interface creation and implementation, encapsulation, and polymorphism.
class Animal{ //Dog class will extend this base class
 public void makeNoise(){}; 
}

public interface Pet{ //Dog class will implement this interface
  void wagTail();
}

class Dog extends Animal implements Pet{ 
 private String noise = "woof woof";
 public void makeNoise(){  //This method overrides method in Animal
  System.out.println(noise);
 }
 public void wagTail(){  //This method is implemented from Pet
  System.out.println("Wagging Tail");
 }
}

class Vet
{
 public void giveShot(Animal a) //Polymorphism here takes Animal or Dog(inherited from Animal)
 {
  a.makeNoise(); //Outputs "woof woof"
 }
}

class GoVet //Starts the program (contains static main method)
{
  private String leaveVet="All done! Leaving vet!"; //Only GoVet has access to this private variable

  public static void main(String [] args) //Starts the program
  {
    Dog d = new Dog();
    Vet v = new Vet();
    v.giveShot(d); //Notice that we send a Dog not an Animal(See Vet class) - polymorphism
    GoVet go = new GoVet(); //Create instance of this same class since we are currently in a static method.
    System.out.println(go.leaveVet);
    d.wagTail(); //Outputs "Wagging Tail"
  }
}
Hosted by www.Geocities.ws

1