The difference between abstract classes and interfaces

Abstract class Abstract class

An abstract class is used to modify the abstract, the abstract class can be used as a template, abstract class can not create an instance of the object can only be inherited as a base class, abstract class method can not abstract, but abstract methods must abstract class, subclass inherits need to rewrite a specific implementation of the abstract method of the abstract class;

Several features abstract class :

Inheritance is : abstract class is mainly used as a base class to be inherited, and subclasses need to override the abstract method implementation of the abstract class, so the method must be modified abstract public, protected

Abstract : abstract class can not be instantiated as a base class, subclass the abstract methods which override, upcast

Note: The subclass must implement all of the abstract methods in an abstract class, if there is no abstract methods to achieve such must be abstract class, subclass can only inherit an abstract class;

interface

Interface is a set of abstract methods, an abstract class is a special interface to modify the general use interface, the abstract interface methods to default is to use public abstrct modified interface public static final variables modified default;

The difference between abstract classes and interfaces

1, and the interface can not be instantiated abstract classes, defined using the interface modifier abstract class uses the abstract interface

2, all of the interface methods are public abstrct modified, can not have non-abstract methods, abstract classes can have non-abstract methods

3, implement the interface is implemented, a subclass can implement multiple interfaces, but only extend inherit an abstract class, but subclass must implement all abstract methods;

4, abstract class can have its own member variables, member variables can be redefined in a subclass or re-assignment, but variables defined in the interface must be public static final modified, it must be given an initial value, and the initial value can not be modified ;

5, there may be an abstract class static methods, static methods and interfaces have not;

Application interfaces and abstract classes

1, can be used as an interface between the various system protocol format of each transmission, a good definition of the parameters, the result set, the specific service implementation in a subclass;
2, abstract class is a template, a method to deal with all subclasses extraction out processing, and if there are specific business differences words on withdrawal abstract method;
3, if the interface to increase the abstract method, all subclasses need to implement this method, if the same treatment in all methods may use an abstract class, adding a non-abstract methods in an abstract class, all subclasses can use this method, but this method without replication;

Published 15 original articles · won praise 7 · views 2859

Guess you like

Origin blog.csdn.net/xiaocaodeshengri/article/details/100690307