I was reading a blog post here: http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
public interface Actor{
Performance say(Line l);
}
public interface Director{
Movie direct(boolean goodmovie);
}
public interface ActorDirector extends Actor, Director{
...
}
It says: In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes.We could achieve the same thing using abstract classes. Unfortunately the alternative would require up to 2^n (where n is the number of attributes) possible combinations in order to support all possibilities.
Question: why abstract class is better here ? and why 2^n ?
public abstract class ActorDirector implements Actor,Director{
}
You cannot inherit from more than one class (abstract or not), but can implement multiple interfaces
If you were to create abstract classes, you will need one for every combination (n atributes: 2^n possible combination) -- to ensure that each combination can be inherited from, if needed.
If you are using interfaces, you only need to define the n interface (one for each attribute) and implement them as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With