Why in this case use the interface over the abstract class?

I read 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 fact, there are Actors who are also Directors. If we use interfaces, not abstract classes. We could achieve the same using abstract classes. Unfortunately, the alternative will require up to 2 ^ n (where n is the number of attributes ) of possible combinations to support all the possibilities.

Question: why is an abstract class better here? and why 2 ^ n?

public abstract class ActorDirector implements Actor,Director{
}
+3
source share
4 answers

, : , - , , .

2^n ( 2^n-1), , n, 2^n-1 . , , , : , , 2^n-1. , , , , Java .

+1

( ),

, (n : 2 ^ n ) - , , .

, n- ( ) .

+6

, , , Abstract ,

- , , ? , AbstractClass.

, Interface , .

, . -, , Abstract, .

, , .

+1

:

  • , .
  • , .

, , , , , . , : , . , (, , ) .

. , Steer (, CompactCar), AttachFollower (, RailwayLocomotive), (, PickupTruck). PickupTruck , , Steer, , , AttachFollower. Steer AttachFollower , PickupTruck, . , - "SteerableAndHitchable abstract class which inerits from Steerable and includes a TrailerHitch member of typeAttachable`, but such a icky thing even with two abilities. Adding more abilities in a safe type will not only require an exploding number of classes; each class will require an extension of the number of properties.

0
source

All Articles