Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when to use Skeletal implementation in java

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 ?

like image 891
peter Avatar asked Nov 18 '12 02:11

peter


People also ask

What is skeletal implementation in Java?

Skeletal implementation is a design by which we can use the benefits of the interface and abstract class together. The Java Collection API has adopted this kind of design: AbstractSet, AbstractMap, etc. are examples of Skeletal interfaces.

Why do we use implementation in Java?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).

When to use interface vs abstract class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Why are interfaces used in Java?

In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.


Video Answer


1 Answers

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

  1. You cannot specify any implementation details about your methods
  2. variables specified at interface level must be static.
  3. 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

  1. Java does not support multiple inheritance, so deriving the benefits of multiple classes, abstract or otherwise is not possible
  2. 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

  1. 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
  2. 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
  3. 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
like image 192
kolossus Avatar answered Nov 11 '22 04:11

kolossus