您的位置:首页 > 编程语言 > Java开发

[Java]--Abstract Class vs Interfaces

2016-08-20 13:24 387 查看

1. Abstract Class

Definition

An abstract class is a class that is declared abstract—it may or may not include abstract methods.

An abstract classes cannot be instantiated.

An abstract classes can be subclassed.

If a class include abstract methods, the class itself must be declared abstract.

Some Code example:

public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}


2. Abstract Methods

The subclass of the abstract class must implement all the abstract methods in its parent class. IF NOT, then the subclass must be declared abstract too

3. Interface

Interface cannot be instantiated either.

Interface may also contain a mix of methods declared with or without implementation .

All fields are automatically
public, static
, and
final
.

All methods that you declare or define (as default methods) are public.

Methods in an
interface
(see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Reference

https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

http://stackoverflow.com/questions/1320745/abstract-class-in-java
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: