From Effective Java Item 18 where it suggests we can combine interfaces and abstract classes by providing an abstract skeletal implementation class. I am wondering when should we use skeletal implementation for simulated multiple inheritance ? and what are the advantages and disadvantages ? Can someone give an example ?
1 Answer
The point of a "Skeletal class" as you put it is mainly to enforce certain ground rules that will govern the use of your design. By themselves, interfaces and abstract classes cannot do the job. Consider the interface for example
- You cannot specify any implementation details about your methods
- variables specified at interface level must be static.
- Interfaces cannot implement anything (class or interface)
So this means that using an interface alone to specify the rules of a design will still permit abuse of the operations defined in your interface, you'll not be able to specify important class level variables or take full advantage of inheritance.
Now take the abstract class
- Java does not support multiple inheritance, so deriving the benefits of multiple classes, abstract or otherwise is not possible
- You cannot derive the full benefit of polymorphism from an abstract class based design only as interfaces (to me) are at the top of the polymorphism tree
Combining both gives you the best of both worlds in that
- An abstract class can implement an interface. An implementing class can extend your abstract class to derive the benefits of already implemented methods of the interface. And this chaining can go on for as many abstract classes as necessary, up till a concrete implementation
- An implementing class can implement as many interfaces as necessary for marker-type purposes. So you can have as many
Serializable
type interfaces for marking that will be extended by other interfaces - A class implementing an abstract class (that implements an interface) can be defined as a type of the interface to benefit from better polymorphism while being instantiated as a child of the abstract class
Consider this use case
- You have an interface that has about say, 6 methods that will govern the use of a class then you have a marker interface that you need simply for identification purposes, so you can use maybe instanceOf operator on.
- Now in that first interface, you need to enforce a specific implementation of just 3 of those methods, the remaining 3, you're going to leave to the end users of the class.
- So interface A has 6 method definitions, Interface B is just a marker for a class to say "I'm a type B class". Now you'll have class A, an abstract class, implement both interfaces A and B
- In your abstract class, you implement just the desired 3 methods, Then you leave the implementation details of the other 3 methods to the final consumers of that abstract class
-
Can you give an example like how does it enforce certain ground rules that will govern the use of design.– peterNov 18, 2012 at 2:47
-
-
what do you mean interfaces cannot implement anything ? interface can extends other interfaces, right ?– peterNov 18, 2012 at 2:54
-
@user1389813,
implements
is notextends
. An interface cannotimplements
another interface (of course), but yes, canextends
, which still leaves it a shell effectively– kolossusNov 18, 2012 at 2:56 -
1