Abstract class:
Abstract class embodies the idea of data abstraction and is a mechanism to realize polymorphism. It defines a set of abstract methods that are implemented by derived classes as a concrete representation of this set of abstract methods. At the same time, abstract classes provide the concept of inheritance, the starting point is to inherit, otherwise it does not exist any meaning. Therefore, the definition of the abstract class must be used to inherit, at the same time in an abstract class as a node of the inheritance relationship hierarchy chain, the leaf node must be a specific implementation class.
In terms of syntax:
1. Classes that are modified by the abstract keyword are called abstract classes.
2. A method that is not implemented in an abstract class is called an abstract method, and the keyword abstract is also required.
3. Abstract classes can also have no abstract methods, such as the HttpServlet method.
4. An abstract class can have a method that has already been implemented, and a member variable can be defined.
Interface:
An interface is used to establish a protocol between classes, which provides only a form, without a specific implementation. The implementation class that implements the interface at the same time must implement all the methods of the interface by using the Implements keyword. Interface is an extension of the abstract class, Java in order to ensure that data security is not multiple inheritance, that is, inheritance can only exist a parent class, but the interface is different, a class can implement multiple interfaces at the same time, regardless of whether there is any relationship between these interfaces, so the interface to compensate for the abstract class can not be multiple inheritance defects,
Grammatical aspects:
1. Called interface by interface keyword modification;
2. Member variables can be defined in an interface, but these member variables are constants of public static final by default.
3. There are no implemented methods in the interface, all of which are abstract methods.
4. A class implements an interface and must implement all the methods defined in the interface.
5. A class can implement multiple interfaces.
Difference:
At the grammatical level: as described above.
Two. At the design level:
1. Different levels of abstraction. An abstract class is an abstraction of a class, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class.
2, cross-domain different. An abstract class spans a class that has similar characteristics, whereas an interface can span different classes of domains. We know that the abstract class is discovering the public part from the subclass, and then generalizing it into an abstract class, and the subclass inherits the parent class, but the interface is different. The subclass that implements it can have no relationship, in common. For example, cats and dogs can be abstracted into an abstract class of animals, with a method called. Birds, airplanes can achieve flying fly interface, with the behavior of flying, here we can not be birds, airplanes share a parent class bar! So the abstract class embodies an inheritance relationship, in order to make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in nature. For an interface, it is not required that the implementation of the interface and the interface definition are consistent in concept, but only the contract that implements the interface definition, which is equivalent to the "like-a" relationship.
3, the design level is different. For the abstract class, it is designed from the bottom up, we have to know the subclass to abstract out the parent class, and the interface is different, it does not need to know the existence of subclasses, only need to define a rule, as to what sub-class, when how to implement it all do not know. For example, we only have a cat class here, if you are abstract into an animal class, is not the design a bit excessive? We have at least two animals, cats, dogs here, we are in the abstract what they have in common to form an animal abstract class it! So, abstract classes are often reconstructed by refactoring! But the interface is different, for example fly, we do not know what will be to achieve this flight interface, how to realize it is not known, we have to do is to define the pre-flight behavior interface. So the abstract class is a bottom-up abstraction, and the interface is designed from the top down.
Additional, NULL interface function:
Usually as a marker, you like this serializable, cloneable.
Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:
* 接口中所有的方法隐含的都是抽象的。而抽象类则可以同时包含抽象和非抽象的方法。* 类可以实现很多个接口,但是只能继承一个抽象类* 类如果要实现一个接口,它必须要实现接口声明的所有方法。但是,类可以不实现抽象类声明的所有方法,当然,在这种情况下,类也必须得声明成是抽象的。* 抽象类可以在不提供接口方法实现的情况下实现接口。* Java接口中声明的变量默认都是final的。抽象类可以包含非final的变量。* Java接口中的成员函数默认是public的。抽象类的成员函数可以是private,protected或者是public。* 接口是绝对抽象的,不可以被实例化。抽象类也不可以被实例化,但是,如果它包含main方法的话是可以被调用的。
The difference between Java abstract class and interface