Abstract Class and Interface 抽象类与接口的区别

abstract Methods and Classes ---抽象方法和抽象类

An abstract class is a class that is declared abstract,it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
以abstract声明的类就是抽象类,抽象类可以有或者没有抽象方法。抽象类不能实例化,但他们可以被子类化。
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
抽象方法就是被声明为没有实现的方法的(没有大括号,直接以分号结束),如下所示
abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declared abstract, as in:
假如一个类有抽象方法,那这个类也必须声明为抽象的。如下所示
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
当抽象类被子类化时,子类通常要提供所有父类中的抽象方法的实现, 假如没有这样做,那么子类也必须声明为抽象类。
Note: All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be it's just not necessary).
注意:所有接口中的方法(可参考接口部分)默认就是抽象的,所以abstract这个修饰符不用使用(他已经是了,所以不需要了)
Abstract Classes versus Interfaces ----抽象类VS接口
Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
不同于接口,抽象类可以包含一二不是static和final的域,他也可以包含实现的方法。这样的抽象类与接口类似,除了他们提供了部分实现,让子类完成实现。假如抽象类只包含抽象方法,他应该被声明为接口,而不是抽象类。
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.
类层次可以实现多个接口,不管这些接口在那个层次上是相关的,例如接口Comparable ,Cloneable
By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

通过比较,抽象类通过子类化时要共享一些实现。单个的抽象类被类似的类子类化,这些类似类有许多相同点(抽象类的实现部分),也有一些不同点(抽象方法)。

你可能感兴趣的:(abstract class)