Abstract classes and interfaces relations

Original link: http://www.cnblogs.com/tuanz/p/8709021.html

Source:  http://blog.xiaohansong.com/2015/12/02/abstract-class-and-interface/

  • Comparison of abstract classes and interfaces
  • Discusses the application of abstract classes and interfaces from the design of the container class java
  • When to use abstract classes and interfaces

Comparison abstract classes and interfaces

parameter Abstract class interface
The default method implementation It can have a default way to achieve Interface is completely abstract. It simply does not exist realization  
achieve Subclasses extends keyword to inherit the abstract class. If a subclass is not an abstract kind of thing, it needs to provide an implementation method of abstract class for all declarations. Subclasses to implement the interface using the keyword implements. It provides the interface required to achieve all the stated methods
Constructor An abstract class can have a constructor Not have an interface constructor
And the difference between normal Java class Except that you can not instantiate abstract class outside normal Java class, and it does not make any difference Interface is a completely different type
Access modifier Abstract methods can have public, protected and default these modifiers The default interface method modifier is public. You can not use other modifier.
main method Abstract methods can have a main method and we can run it The interface is not the main method, we can not run it.
Multiple Inheritance Abstract methods can inherit a class and implement multiple interfaces Interfaces can inherit only one or more other interfaces
speed It is faster than the speed of the interface The interface is a little bit slow, because it takes time to find ways in the class implementation.
Add a new method If you add new methods to an abstract class, you can provide default implementations for it. So now you do not need to change your code. If you add a method to the interface, then you must change the class that implements the interface.

Abstract classes and interfaces have different, there are similar places. Some books to an interface called the special class, though not accurate, but it may have merit, the interface can do, an abstract class can do, in addition to multiple inheritance. Because of these similar characteristics, so we have a confused when to use them: here in the end of the abstract classes or interfaces?

Discusses the application of abstract classes and interfaces from the design of the container class java

In addition to the previously mentioned a problem: in the end is a polymorphic interface or abstract class? I also saw someone commented that: are now advocating oriented programming interface, using the old code abstract class farmers have been called the last century. Haha. I also see this argument can not smile. But oriented programming interface is indeed a trend, java 8 already supports the default interface method and static method, the difference between abstract classes and interfaces more and more. Without further ado, we begin to discuss the application of abstract classes and interfaces.

full_container_taxonomyfull_container_taxonomy

The figure is a class hierarchy of container class java. Our container class  ArrayList as an example to discuss the application of abstract classes and interfaces.

ArrayList class inheritance

ArrayListArrayList

The figure is  ArrayList a class hierarchy. It can be seen ArrayList inheritance used in both abstract classes, use the interface.

  • Top-level interface is  Iterableto indicate that this is the type can be iterative. All container classes are iterative, this is a highly abstract.
  • The second layer is the interface  Collection, which is the interface of the single elements of the container. Sets, lists fall into this category.
  • The third layer is the interface  List, which is a list of all interfaces.

Through three interfaces, we can find three abstract properties of container classes implement these interfaces means that properties have these interfaces.

  • AbstractCollection It implements  Collection part of the process.
  • AbstractList Achieved  AbstractCollection and a  List portion of the process.

The above abstract class provides default implementations of some methods, to provide a specific class code reuse.

Pure abstract class implements

If we like an old farm yard, like abstract classes to implement what will be the effect of the above interfaces? So, class diagram may become so.

AbtractListAbtractList

Abstract classes where there is a big problem, it can not be multiple inheritance, no interfaces on the high level of abstraction, nor flexible interface. for example:

List + AbstractCollection -> AbstractList
Set  + AbstractCollection -> AbstractSet

Simply use the abstract class can not be achieved as flexible as interface extensions.

Pure interface

If we like a new code as agriculture, pure interface to achieve it?

InterfaceListInterfaceList

Write theory there is no problem, actually writing code when the question came. All interfaces must be provided to achieve, so you have to duplicate code in each implementation class.

to sum up

After the above discussion, we have come to two conclusions:

  • Abstract classes and interfaces can not replace each other.
  • Abstract classes and interfaces have an irreplaceable role.

Can be seen from the class diagram container class, the interface of the abstract type is mainly used in common, say for example, the container may be iterative nature. The main abstract class is to provide a reusable code embodied classes, such that List some of the default method.

When to use abstract classes and interfaces

So, when to use abstract classes, interfaces when to use it?

To solve the above problem, we start to figure out the relationship between abstract classes and interfaces. First of all, we all know of things abstract class that defines the properties and behavior of things. Abstract class is incomplete class, abstract method. Interface is even higher than the level of abstraction class. So, we can understand the relationship between them: is the abstract of things, the abstract class is an abstract class, an interface is an abstract class abstract.

From this perspective, java container class, you will find that it is reflected in the design of this relationship. Is not it? From  Iterable the interface, the  AbstractList abstract class, to the  ArrayList class.

Now to answer the previous question: in the design category, first consider the characteristics of the interface with the abstract class when you find some ways to reuse, they can use an abstract class to reuse code. Simply put, the interface characteristics for abstract things, abstract class for code reuse .

Of course, not all classes should be designed from the interface to the abstract class, to the class. This is absolutely no programming paradigm to follow. The above statement is just to provide a perspective to understand the relationship between abstract classes and interfaces, each person will have their own understanding, some people think that the two had nothing to do, it also makes sense. In short, patterns and grammar is dead, people are living.

 

Reproduced in: https: //www.cnblogs.com/tuanz/p/8709021.html

Guess you like

Origin blog.csdn.net/weixin_30216561/article/details/94785899