Effectivejava Reading notes-interface is better than abstract class

Source: Internet
Author: User

The Java programming language provides two mechanisms that can be used to define types that allow multiple implementations: interfaces and abstract methods, both of which are directly drunk as the obvious difference is that abstract classes allow implementations of certain methods, but interfaces do not allow, a more important difference is that in order to implement types defined by abstract classes, A class must be a subclass of an abstract class. Any class that defines all the necessary methods and adheres to a common convention is allowed to implement an excuse, regardless of where the class is at the class level. Because Java only allows single inheritance, all abstract classes are constrained by the class as a type definition.

  The existing classes are easily updated to implement the new interface.

If you want to have two classes extend the same abstract class, you must place the abstract class at the height of the type hierarchy so that one ancestor of the two classes becomes its subclass. Unfortunately this is done indirectly to the level of the class, forcing the common ancestor to all descendant classes to extend the new abstract class, regardless of whether it is appropriate for these descendant classes.

An interface is an ideal choice for defining mixed types.

Interfaces allow us to construct a type framework that is not hierarchically structured.

  If we have two interfaces, one for singers, another for composers, in real life, there are a lot of people are singers and composers, if it is an interface, I just need to implement both interfaces at the same time, if it is abstract class, because Java is a single inheritance, I have no way to describe this kind of people.

Although interfaces do not allow implementations of methods, using interfaces to define types does not prevent you from providing implementation assistance for your program apes. by providing an abstract skeleton implementation class for each important interface you export, combine the advantages of the interface and the abstract class. the function of the interface is still the definition type, but the implementation class of the skeleton takes over all the work related to the interface implementation.

The skeleton provides the interface with help on the implementation, but does not impose strict restrictions that are unique to the "abstract class is used as a type definition." For most implementations of interfaces, an extended skeleton implementation class is an obvious choice, but not a necessity. If the prefabricated class cannot extend the skeleton implementation class, this class can always implement this interface. In addition, the skeleton implementation class is still helpful to the implementation of the interface. The class that implements this interface can forward a call to this interface method to an instance of an internal private class that extends the skeleton implementation class. This approach is known as simulating multiple inheritance. This technology has a large majority of multiple inheritance, while avoiding the corresponding flaws.

Writing a skeleton implementation class is relatively simple, just a bit tedious. First, it is important to study the interfaces carefully and determine which methods are the most basic, and other methods can be implemented according to them. These methods will become abstract methods in the skeleton implementation class. Then, you must provide a concrete implementation for all the other methods in the interface. For example, the following is the skeleton implementation class for the Map.entry interface:

/** * interface is better than abstract class * @author Weishiyao * * @param <K> * @param <V> *///skeletal implementationpublic abstract Clas S abstractmapentry<k, v> implements Map.entry<k, v>{//Primitive operationspublic abstract K getKey ();p ublic Abstract v getValue ();//Entries in modifiable maps must override this methodpublic V setValue (V value) {throw new Unsuppo Rtedoperationexception ();}  Implements the general contract of Map.entry.equals@overridepublic Boolean equals (Object obj) {if (obj = = this) {return true;} if (! ( obj instanceof Map.entry)) {return false;} map.entry<?,? > arg = (map.entry) Obj;return equals (GetKey (), Arg.getkey ()) && equals (GetValue (), Arg.getvalue ());} private static Boolean Equals (object Obj1, Object obj2) {return obj1 = = null? obj2 = = Null:obj1.equals (OBJ2);} Implements the general contract of Map.entry.hashcode@overridepublic Int hashcode () {return hashcode (GetKey ()) ^ Hashco De (GetValue ());} private static int hashcode (Object obj) {return obj= = null? 0:obj.hashcode ();}}

Skeleton implementation class in order to inherit the purpose of design, this use in peacetime development often can be seen, many frameworks are used in the way, before is also seen so many times the framework so used, roughly understand what it is doing, this time after so detailed explanation, is thoroughly understand why to use, The person who can think of such a writing is really a great God.

  

  

Effectivejava Reading notes-interface is better than abstract class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.