Ejercicio De Patos Con Herencia Interfaz Y Polimorfismo

Creacion de la interfaz Flyable


// @author Misael & Karina

public interface Flyable {
void fly();
}

Creación de las clases que implementan la interfaz Flyable


public class Nofly implements Flyable {
 
           public void fly()
            {
          System.out.println("No vuela");
            }
       }
 
    public class FlyWhitWings implements Flyable {
 
            public void fly()
        {
            System.out.println("El pato utiliza alas para volar");
            }
    }
 
    public class FlyAlone implements Flyable {
 
            public void fly()
            {
            System.out.println("Vuela solo");
            }
    }

Creacion de la interfaz Quackable

public interface Quackable {
    void quack();
    }

Creación de las clases que implementan la interfaz Quackable

public class StrngQuack implements Quackable{
 
            public void quack(){
           System.out.println("Quackkkkkkkkkkk");
        }
    }
 
    public class NotQuack implements Quackable {
 
        public void quack(){
        System.out.println("Not Quack");
        }
    }
 
    public class WeakQuack implements Quackable{
 
           public void quack()
            {
            System.out.println("Quackk");
            }
    }
 
    public class Squezz implements Quackable {
 
            public void quack()
            {
            System.out.println("squezzzzzzz");
            }
    }

Creación de la Clase Pato

public class Pato {
 
    protected Flyable obj1;
    protected Quackable obj2;
 
    Pato(Flyable obj1,Quackable obj2)
    {
    this.obj1=obj1;
    this.obj2=obj2;
    }
 
    public void swimm()
    {
        System.out.println("nada");
    }        
 
    public void mostrar()
    {
    obj1.fly();
    obj2.quack();
    }
 
}

Creación de las clases que heredan de la clase Pato

public class WoodenDuck extends Pato {
    protected String name;
 
    WoodenDuck(Flyable obj1,Quackable obj2,String name){
        super(obj1,obj2);
        this.name=name;
   }
 
   public void getqualitys(){
 
   System.out.println(name);
   super.swimm();
   super.mostrar();
 
   }
 
}

public class MallorDucks extends Pato {
    protected String name;
 
    MallorDucks(Flyable obj1,Quackable obj2,String name)
    {
    super(obj1,obj2);
    this.name=name;
    }
 
    public void getqualitys(){
 
        System.out.println(name);
        super.swimm();
        super.mostrar();
 
    }
}

public class ReadHeadDucks extends Pato {
 
    String name;
    ReadHeadDucks(Flyable obj1,Quackable obj2,String name)
    {
        super(obj1,obj2);
        this.name=name;    
    }
 
     public void getqualitys()
     {
       System.out.println(name);
       super.swimm();
       super.mostrar();       
     }
}

public class RubberDuck extends Pato {
 
    String name;
    RubberDuck(Flyable obj1,Quackable obj2,String name)
    {
        super(obj1,obj2);
        this.name=name;    
    }
 
     public void getqualitys()
     {
       System.out.println(name);
       super.swimm();
       super.mostrar();       
     }
 
}

Creacion de la clase de prueba

public class TestHducks {
 
        public static void main(String[] args) {
 
        Quackable quackobj2=new NotQuack();
        Quackable quackobj1=new StrngQuack(); 
        Quackable quackobj3=new WeakQuack();
        Quackable quackobj4=new Squezz();
 
        Flyable flyobj1=new Nofly();
        Flyable flyobj2=new FlyWhitWings();
        Flyable flyobj3=new FlyAlone();
 
        WoodenDuck s1=new WoodenDuck(flyobj1,quackobj2,"pedro de madera");
        ReadHeadDucks s2=new ReadHeadDucks(flyobj3,quackobj3,"luis de cabeza roja");
        MallorDucks s3=new MallorDucks(flyobj2,quackobj1,"jorge comun");
        RubberDuck s4=new RubberDuck (flyobj1,quackobj4,"paco de hule");
 
        s1.getqualitys();
        s2.getqualitys();
        s3.getqualitys();
        s4.getqualitys();
 
    }
 
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License