1. Adding an abstract method "fly" into the class Animal. Then, I force the sub-classes should be implemented this method, something like:
public abstract class Animal{
//bla bla
public abstract void fly();
}
public class Bird extends Animal{
//bla bla
public void fly(){
System.out.println("Fly high");
}
}
public class Dog extends Animal{
//bla bla
public void fly(){
System.out.println("Cant fly");
}
}
2. Creating an interfaces with method "fly" inside. The same issue to abstract class, I force the classes these implement this interface should have a method "fly" inside:
public interface Flyable{
public void fly();
}
public class Bird implements Flyable{
//bla bla
public void fly(){
System.out.println("Fly high");
}
}
public class Dog implements Flyable{
//bla bla
public void fly(){
System.out.println("Cant fly");
}
}
These approaches are the OOP basics in order to solve the problem when we want to add a new behavior, but there is a disadvantage because all sub-classes are forced to implement this behavior even it doesn't make sense for some classes. And, there is no reused code by using interfaces only.
That is where Strategy Design Pattern can help. The following is an example code:
public interface Flyable{
public void fly();
}
public class ItFlys implements Flyable{
public void fly(){
System.out.println("Fly high");
}
}
public class CantFly implements Flyable{
public void fly(){
System.out.println("Cant fly");
}
}
public abstract class Animal{
//bla bla
Flyable flyType;
public void tryToFly(){
flyType.fly();
}
public void setFlyAbility(Flyable newFlyType){
flyType = newFlyType;
}
}
public class Bird extends Animal{
//bla bla
public Bird(){
flyType = new ItFlys ();
}
}
public class Dog extends Animal{
//bla bla
public Dog (){
flyType = new CantFly ();
}
}
This is a diagram that shows the example above:
src: https://www.youtube.com/watch?v=-NCgRD9-C6o&index=3&list=PLF206E906175C7E07
References: