java foundation: 10, the difference between the ordinary class and abstract class? The difference between interfaces and abstract classes? An abstract class must be abstract way? Abstract classes can use the final modified it? (Super detail)

1, the difference between the ordinary class and abstract class?

Abstract class is a class abstract method comprising, wherein some of the methods declared abstract to represent a class is abstract. An abstract class is a template pattern, subclasses can be expanded on this template. There is an abstract class is a subclass of design also avoid arbitrariness. To achieve separation of code design and implementation.

(1) An abstract class can not be instantiated, not a new instance of the object.
(2) as long as the class of the abstract method, must be defined as an abstract class.
(3) An abstract class may contain attributes, methods, construction method. However, subclasses can call the constructor, not be used to instantiate the object.
(4) can be used to abstract class inheritance, and its subclasses must override abstract method, unless the subclass itself is an abstract class.

NOTE: There may be a common method in the abstract class. Abstract method only method declaration, there is no method body.

2, the difference between interfaces and abstract classes?

Interfaces and abstract classes are supported by two mechanisms abstract class definition.
Abstract class is a class abstract method comprising, wherein some of the methods declared abstract to represent a class is abstract.
The interface is a collection of a process in which all the methods are not methods body, be implemented by a keyword implements. Interface only constants and abstract methods.
The same point (2 points):
(1) abstract classes and interfaces can not be instantiated.
It can be instantiated after (2) and subclasses of abstract classes implement interfaces are realized only in the method of the abstract class or interface.

Different points (6 points):
the definition and implementation of (1)
Before java8, you can only define the interface methods, can not be achieved, only the implementation class that implements the interface method can be achieved. However, the method can abstract class definition + implemented.
(2) Implementation and inheritance.
Interface with the implements, the abstract class can only inherit extends. A class can implement multiple interfaces, but can only inherit an abstract class. Therefore, the interface can indirectly achieve multiple inheritance purposes.
(3) design.
Interface emphasis on the achievement function, design philosophy: has - a; abstract classes emphasize affiliations, design concept: is - a.
(4) differs from member variables.
Interface member variables default to public static final, only have static data member can not be modified, and must be assigned an initial value to it.
An abstract class can have its own data member variables. Abstract class member variables default to default, of course, can also be defined as public, protected and private, the members of these variables can be redefined in a subclass, it can also be reassigned.
(5) differs member methods.
Members of the interface methods can only be public abstract, and can only be modified both keywords.
Non-members can have an abstract method in the abstract class. Abstract method of the abstract class (abstract modification has front) can not use private, static, synchronized, native modifications and other modifiers. At the same time must end with a semicolon, you can not have braces.
(6) applications.
The interface is used in the more common features to facilitate future maintenance or increase the delete method.
Abstract classes are more inclined to act as a public role, it does not apply to later re inside the code to be modified.

Other notes:
Interface is a special kind of abstract class. Interface can be inherited interfaces.
An abstract class can implement an interface, an abstract class can inherit specific class. An abstract class can have static main method.

3, an abstract class must be abstract way?

(1) there must be an abstract class abstract modification.
(2) does not have to abstract method of the abstract class.
(3) There must be an abstract method of the abstract class class.

4, abstract class can use the final modified it?

Not.
With a final modified class can not be inherited.
An abstract class is a template pattern, subclasses can be expanded on this template. There is an abstract class is a subclass of design also avoid arbitrariness. To achieve separation of code design and implementation. Which is an abstract method to be implemented by the subclasses. The final modification of the abstract class approach defeats the purpose of an abstract class.

Published 57 original articles · won praise 13 · views 1133

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105053433