`
i_am_birdman
  • 浏览: 275273 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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
http://blog.csdn.net/russle/archive/2009/08/16/4453777.aspx
分享到:
评论

相关推荐

    Java中抽象类和接口的区别

    在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,...

    详细解析Java中抽象类和接口的区别

    抽象类和接口的区别 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。abstract class和interface之间在对于抽象类定义的支持...

    Java中抽象类和接口的区别与作用详解

    结论:abstract class和Interface是JAVA语言中的两种定义抽象类的方式。Interface将abstract的概念作了更进一步的发挥。你可以想像Interface是“纯粹”的 abstract class,只可以包含抽象方法,也包括一些常量的定义...

    深入理解abstract class和interface

    abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstract class和interface的选择显得比较随意。其实,两者之间还是有很大的...

    JAVA中抽象类与接口的区别

    在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性...

    JAVA中抽象类和接口的区别

    在Java语言中,abstract class 和interface 是支持抽象类定义的两种机制区别

    1.5:接口和抽象类的区别.pdf

    4.实现抽象类和接口的类必须实现其中的所有方法。 抽象类中可以有非抽象方法。接口中则不能有实现方法。如果接口或者抽象类的子类不想实现则继续携程一个抽象方法 5.接口中定义的变量默认是public static final ...

    Java中抽象类和接口的区别.zip_java interf

    在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,...

    C++中抽象类和接口的区别介绍

    抽象类(abstract class)和接口(interface)的概念是面向对象设计中常用的概念, 也是比较容易混淆的概念. 在这里, 我提出一种区分它们的思路

    浅析Java抽象类和接口的比较

    abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。

    Java学习笔记---15.面向对象编程10-Java中final关键字,抽象类与接口

    好啊好啊 抽象类与接口是Java中最重要部分之一,这里用较大的篇幅来做下这部分的笔记. 1. final关键字 在Java中, 可以使用final关键字修饰类、方法以及成员变量。 (1).final标记的类不能被继承; (2).final标记...

    Java抽象类与接口的区别

    含有abstract修饰符的class 即为抽象类,abstract类不能创建实例对象,含有abstract的方法的类必须定义为abstract class ,abstract class 里的方法不必是抽象的,抽象类中定义抽象方法必须放在具体子类中实现,所以...

    Java中interface接口与abstractclas

    Java中interface接口与abstractclass抽象类的区别共2页.pdf.zip

    abstract class

    abstract class 抽象类--不具体的类 1) 抽象方法,只有行为的概念,没有具体的行为实现。 2) 包含抽象方法的类,就一定是抽象类。 使用: abstract 关键字修饰,包含抽象方法。 如:平面图形一定可以计算面积。 ...

    java高级类操作 接口与抽象类的操作

    circle Rectangle等类在接口中计抽象类中的用法 抽象类 接口多态 public class Polymorphism { public static void main(String args[]) { Triangle t=new Triangle(5.0,2.0); t.show_area(); Rectangle r=...

Global site tag (gtag.js) - Google Analytics