abstract class
Java
Are allowed abstract
modifiers method declaration, but this time not only define the method implementation method ( abstract
modified method has no body, only a signature and a semicolon).
The following is the abstract
method and location of these methods abstract
associated with the class rules:
- As long as there is a class
abstract
method, then the class itself will automatically becomeabstract
, and must be declaredabstract class
, otherwise it will cause a compiler error; abstract class
Can not be instantiated;abstract
Subclasses must override each super class classabstract
method, and achieve all these methods (i.e., methods providing the body), can be instantiated. This class generally known as concrete subclasses (concrete subclass
), in order to stress that it is not an abstract class.- If a
abstract class
child class does not implement all the integrationabstract
method, then this sub-category is stillabstract class
, and must use theabstract
declaration; - Use
static
,private
andfinal
declared method is not an abstract method, because the three methods can not be overridden in a subclass like.final class
Can not be anyabstract
method; - Even if not a class
abstract
method, the class can be declaredabstract
. Using this method declarationabstract
class indicates that the class implements imperfect, we need to subclass implementation of this class can not be instantiated.
interface
Java
Single inheritance class inheritance pattern, in order to further extend the object-oriented, Java
creating a interface
concept
interface
is the role of drawing API
, therefore, interface
to provide a description of the type of information, and the realization of this API
method should provide a class.
The following are the interface
relevant rules:
interface
All methods implicitly use forcedabstract
statement, the body can not have a method to use the semicolons (useabstract
modifier, but generally will be omitted);- Public interface definition
API
.interface
All members are implicitly usingpublic
statement, and omitting unnecessary habitpublic
modifier if used in the interface.protected
Alternativelyprivate
defined method causes a compilation error; interface
Can not define any potential instance fields fields are implementation details, and the interface is not a rule in advance, in the interface can only be defined using both.static
Andfinal
constant declarations;- Interfaces can not be instantiated, the constructor and therefore can not be defined;
- Interface type may contain nested nested type used implicitly.
public
Andstatic
declarations; - From the
Java 8
beginning,interface
it can contain static methods.
Difference summary
abstract class
And interface
there are many links, there are also differences.
The following is a summary of the differences in personal expenditure:
abstract class
Aclass
method is not implemented in the present, it left inherited subclass implementation,interface
the method is not implemented in the method are (Java 8
ininterface
thedefault
excluding method);
public interface List<E> extends Collection<E> {
// `interface`中被`default`修饰的方法,提供默认实现
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
}
- Subclass only
extends
a singleabstract class
, butimplements
a pluralityinterface
; abstract class
It can be declarednon final
variable (such asint test;
),interface
the declaration is onlystatic final
modified variable;abstract class
It may be variableprivate
,public
as well asprotected
modifiedinterface
variable only bepublic
modified;intercae
It can beextends
multipleinterface
, andabstract class
can beextends
a class (non-limitedabstract class
) and a pluralityinterface
.
Use coding
In the daily work of coding, choose abstract class
or interface
need to be careful.
abstract class
Represent inheritance, an inheritance relationship is "is a" relationship in nature;interface
It represents a certain behavior relationship islike a
the relationship.
With 带报警器的门
, for example, she has the role of the door, also has an alarm function.
- Inherited door (the door for
abstract class
subclasses implement switching function); - Implement alarm function (implemented alarm
interface
, the alarm function is complete).
PS:
If you think my article helpful to you, you can scan the code to receive support under a red envelope or scan code (random number, a penny is love), thank you!
Alipay red envelope | Alipay | Micro letter |
---|---|---|