This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Duck | |
{ | |
public void swim() | |
{ | |
System.out.println("i can swim."); | |
} | |
public void quack() | |
{ | |
System.out.println("quack quack!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MallardDuck extends Duck | |
{ | |
@Override | |
public void display() | |
{ | |
System.out.println("I'm a mallard duck"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface FlyBehaviour | |
{ | |
void fly(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface QuackBehavior | |
{ | |
void quack(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fly implements FlyBehaviour | |
{ | |
@Override | |
public void fly() | |
{ | |
System.out.println("I can fly."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Duck | |
{ | |
QuackBehavior quackBehavior; | |
FlyBehaviour flyBehaviour; | |
public void performQuack() | |
{ | |
quackBehavior.quack(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MallardDuck extends Duck | |
{ | |
public MallardDuck() | |
{ | |
this.quackBehavior = new Quack(); | |
this.flyBehaviour = new Fly(); | |
} | |
@Override | |
public void display() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Quack implements QuackBehavior | |
{ | |
@Override | |
public void quack() | |
{ | |
System.out.println("Quack, quack!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
Duck duck = new MallardDuck(); | |
duck.swim(); | |
duck.display(); | |
duck.performFly(); | |
duck.performQuack(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RubberDuck extends Duck | |
{ | |
public RubberDuck() | |
{ | |
this.quackBehavior = new Squeak(); | |
this.flyBehaviour = new FlyNoWay(); | |
} | |
@Override | |
public void display() |
OlderNewer