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).


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


When should you use an abstract class, when an interface , when both? Interfaces and abstract classes seem superficially to provide almost the same capability. How do you decide which to use?

When To Use Interfaces


An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface . To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.

When To Use Abstract classes


An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

When to Use Both


You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

Summary Table
[url]http://blog.csdn.net/russle/archive/2009/08/16/4453777.aspx[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值