Home » Java » The difference between abstract class and interfac...

The difference between abstract class and interface, why use an abstract class.

The Internet and learn, down here to see the gold.
An abstract class:

In object-oriented field, mainly used for class abstraction types. What is the type of hidden hidden? We can construct the abstract description of a fixed set of behavior, but this group of behavior but can have any a possible concrete implementation. This is the abstract description of abstract class, and this an arbitrary a the implementation shows all possible derived class. For example, the animal is an abstract class, monkey, tiger is a derived class implementation, we can use the type of animal hide type, the monkey and the tiger. (reference doSome)

Two, interface:

What is the interface? Java interface is a series of methods of the statement, is a collection of feature, feature an interface only method to realize no method, so these methods can be in different places by different class, and these can have different behavior (function) is the interface. Abstract class is a special form of.

Three, the difference between abstract class and interface:

First of all, the abstract class in Java language is a kind of inheritance, a class can only be used once the inheritance relationship. However, a class can implement multiple interfaces. Perhaps, this is the Java language designers in considering Java for a break into senior high school entrance examination multiple inheritance support. Secondly. In the definition, the abstract class, we can give methods of default behavior. But in the interface definition, the method cannot have the default behavior, in order to circumvent this limitation, you must use the client, but it will add some complexity, sometimes will cause a lot of trouble. An abstract class in Java language embodies a kind of inheritance the relationship, in order to make a reasonable inheritance relationship, there must be "is a" relationship between the base class and the derived class, the parent class and derived classes in the essence of concept should be the same. The interface is not, does not require the implementation of interface And the interface definition is consistent in nature, just to achieve the interface definition. The contract interface represents is like
A ". The use of abstract classes to define allows multiple types of fulfillment, there is an obvious advantage over the use of the interface: evolution of the abstract class is easier than the interface evolution. In a subsequent release, if you want a way to increase in an abstract class, just add a reasonable default can be implemented, all implementations of abstract classes are automatically provided for this new method. This interface is not feasible. Although you can achieve a skeleton implementation class increase method to solve some problems, but it cannot solve the problem does not realize the interface implementation class inherited from the skeleton. Thus, the design of the public the interface will be very cautious, once an interface is open and widely implemented, modifies it is impossible. So, using the interface or abstract class, depends on our concept of problem understanding the nature and design intention.
The same Java interface and abstract class:

1. can be instantiated.

2. can contain abstract methods.

Abstract class and interface. It is closely related to interface and abstract class more than abstract, which is mainly reflected in the difference: 1) can achieve unlimited interfaces, but only from an abstract (or any other type) class that implements the interface from the abstract class can be obtained from the interface. Is used to solve the problem of multiple inheritance.2) abstract class can exist non abstract method, method and interface cannot be inside it is a famous must use public to modify no specific methods to achieve the.3) abstract class member variables can be different modifiers to modify, can interface member variables the default is the static constants (static fainl).4) which is also the most important point of the nature of a "abstract class is the object of the abstract, but the interface is a code of conduct".

For example, each interface can represent a top-level abstraction, can be understood as the representative of a class of things, if a class implements an interface. The class has a variety of types, which is an ideal tool for defining the interface of mixed type finally: there is a design pattern, that is, the default adapter pattern, meaning that is, first to define an interface that implements this interface through the abstract class, and the realization method, some subclasses need not necessarily achieve then, subclasses can choose inheritance interface, implement all methods, or directly inherited this abstract class method to realize the specific needs of the

four, why use the abstract class

 define abstract classes to its subclasses later inherit the parent class, because there are a lot of methods is not the definition of the specific implementation, only the definition of a prototype, let subclasses to achieve! So be defined as abstract! 
 simply Namely
Abstract class is a bridge between the interface and the entity class.
for example
An interface is called a flight FlyAnimalAction, inside the definition of a method called flying, and then define a method called eat
Do a class called mosquito mosquito implements the interface, the realization of flying method, eat method to realize their own
A class implements an interface called fly fly, to implement the flying method, eat method to realize their own
You find all the flying animal must implement this interface, very troublesome, as
As an abstract class FlyAnimal, then realize the above interface
The implementation of flying method on the inside, because most of the flight is the same, and the eat method to write abstract methods, because most of the animal food is not the same
Below you write mosquitoes is simple, can extend this abstract class directly, and then realize their way to eat on the line
And fly class is the same, this time the abstract class function on the show, of course, the abstract class function is far from these, just understand here early enough.