Implement two interfaces - Java Object Oriented Design

Java examples for Object Oriented Design:interface

Description

Implement two interfaces

Demo Code

interface Walkable {
  void walk();/*w w  w .j  ava 2 s  .co m*/
}
interface Swimmable {
  void swim();
}

class Turtle implements Walkable, Swimmable{
    private String name;

    public Turtle(String name) {
          this.name = name;
    }

    // Adding a bite() method to the Turtle class
    public void bite() {
        System.out.println(name + " (a turtle) is biting.");
    }
    
    // Implementation for the walk() method of the Walkable interface
    public void walk() {
        System.out.println(name + " (a turtle) is walking.");
    }

    // Implementation for the swim() method of the Swimmable interface
    public void swim() {
        System.out.println(name + " (a turtle) is swimming.");
    }
}


public class Main {
  public static void main(String[] args) {
    Turtle turti = new Turtle("aa");
    letItBite(turti);
    letItWalk(turti);
    letItSwim(turti);  
  }   
  
  public static void letItBite(Turtle t) {
    t.bite();
  }
  
  public static void letItWalk(Walkable w) {
    w.walk();
  }
  
  public static void letItSwim(Swimmable s) {
    s.swim();;
  }
}

Result


Related Tutorials