Herencia + Polimorfismo Patos

Clase principal Duck

public class Duck 
{
    Flyable TF;
    Quackable TQ;
    public void display()
    {
        System.out.println("Se Muestra en pato");
    }
    public void swing()
    {
        System.out.println("El pato nada");
    }
}

Interfaz # 1 Quackable

public interface Quackable
{
    public void quack();
}
public class Squeez implements Quackable
{
    public void quack()
    {
        System.out.println("Squeez");
    }
}
public class No_Quack implements Quackable
{
    public void quack()
    {
        System.out.println("Don´t Speak");
    }
}
public class Quack implements Quackable
{
     public void quack()
    {
        System.out.println("Quack Quack ... Quack");
    }
}

Interfaz # 2 Flyable

public interface Flyable
{
 public void fly();
}
public class No_Fly implements Flyable 
{
    public void fly()
    {
        System.out.println("Don´t Fly");
    }
}
public class Fly_With_Wings implements Flyable
{
 
     public void fly()
    {
        System.out.println("Fly Higt");
    }
 
}

Clases Heredadas con una implementación especifica


Clase Mallord_Duck

public class Mallord_Duck extends Duck  
{ 
    public Mallord_Duck()
    {
        super();
        TF= new Fly_With_Wings();
        TQ= new Quack();
    }
  public void imprimir(){
      System.out.println("Mallord_Duck");
        super.display();
        super.swing();
        TF.fly();
         TQ.quack();
    }      
}

Clase RedHead_Duck

public class RedHead_Duck extends Duck  
{
   public RedHead_Duck()
   {
       super();
        TF= new Fly_With_Wings();
        TQ = new Quack();
    }
 
    public void imprimir () {
       System.out.println("RedHead_Duck");
       super.display();
       super.swing();
       TF.fly();
        TQ.quack();
    }
}

Clase Rubber_Duck

public class Rubber_Duck extends Duck  
{
    public Rubber_Duck()
    {
        super();
         TF= new No_Fly();
         TQ= new Squeez();
        }
    public void imprimir(){
        System.out.println("Rubber_Duck");
        super.display();
        super.swing();
        TF.fly();
        TQ.quack();
    }      
}

Clase Wood_Duck

public class Wood_Duck extends Duck
{
    public Wood_Duck()
    {
        super();
         TF= new No_Fly();
         TQ= new No_Quack();
    }
  public void imprimir(){
       System.out.println("Wood_Duck");
        super.display();
        super.swing();
        TF.fly();
        TQ.quack();
    }      
 
}

Clase Ejecutable o clase Main

public class Implementacion
{
   public static void main(String[] args)
   {
       Mallord_Duck D1= new Mallord_Duck();
       RedHead_Duck D2= new RedHead_Duck();
       Rubber_Duck  D3= new Rubber_Duck();
       Wood_Duck    D4=new Wood_Duck();
       System.out.println(" ");
       D1.imprimir();
       System.out.println(" ");
       D2.imprimir();
       System.out.println(" ");
       D3.imprimir();
       System.out.println(" ");
       D4.imprimir();
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License