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 ?
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.
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 ).
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.
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.
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
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
Combining both gives you the best of both worlds in that
Serializable
type interfaces for marking that will be extended by other interfacesConsider this use case
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