请问抽象类和接口的区别?

samuel2005 2005-01-13 10:38:13
请问各位大哥,抽象类和接口的区别?
...全文
328 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenlinping 2005-02-02
  • 打赏
  • 举报
回复
声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态方法。Abstract 类的子类为它们父类中的所有抽象方法提供实现,否则它们也是抽象类为。取而代之,在子类中实现该方法。知道其行为的其它类可以在类中实现这些方法。
  接口(interface)是抽象类的变体。在接口中,所有方法都是抽象的。多继承性可通过实现这样的接口而获得。接口中的所有方法都是抽象的,没有一个有程序体。接口只可以定义static final成员变量。接口的实现与子类相似,除了该实现类不能从接口定义中继承行为。当类实现特殊接口时,它定义(即将程序体给予)所有这种接口的方法。然后,它可以在实现了该接口的类的任何对象上调用接口的方法。由于有抽象类,它允许使用接口名作为引用变量的类型。通常的动态联编将生效。引用可以转换到接口类型或从接口类型转换,instanceof 运算符可以用来决定某对象的类是否实现了接口。
phele 2005-02-02
  • 打赏
  • 举报
回复
http://www.ossoft.org/FORUM/viewtheme.asp?area=11&lstsort=0&lstday=-1&page=1&id=447
jiangxuhong217 2005-02-02
  • 打赏
  • 举报
回复
原文是这样写的:
What Is an Interface?
This section shows you how to create and to use interfaces and talks about why you would use an interface instead of a class.
An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior.



--------------------------------------------------------------------------------
Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants.
--------------------------------------------------------------------------------


Because an interface is simply a list of unimplemented, and therefore abstract, methods, you might wonder how an interface differs from an abstract class. The differences are significant.


An interface cannot implement any methods, whereas an abstract class can.
A class can implement many interfaces but can have only one superclass.
An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.
Let's set up the example we'll be using in this section. Suppose that you have written a class that can watch stock prices coming over a data feed. This class allows other classes to register to be notified when the value of a particular stock changes. First, your class, which we'll call StockMonitor, would implement a method that lets other objects register for notification:


public class StockMonitor {
public void watchStock(StockWatcher watcher,
String tickerSymbol, double delta) {
...
}
}

The first argument to this method is a StockWatcher object. StockWatcher is the name of an interface whose code you will see in the next section. That interface declares one method: valueChanged. An object that wants to be notified of stock changes must be an instance of a class that implements this interface and thus implements the valueChanged method. The other two arguments provide the symbol of the stock to watch and the amount of change that the watcher considers interesting enough to be notified of. When the StockMonitor class detects an interesting change, it calls the valueChanged method of the watcher.

The watchStock method ensures, through the data type of its first argument, that all registered objects implement the valueChanged method. It makes sense to use an interface data type here because it matters only that registrants implement a particular method. If StockMonitor had used a class name as the data type, that would artificially force a class relationship on its users. Because a class can have only one superclass, it would also limit what type of objects can use this service. By using an interface, the registered objects class could be anything--Applet or Thread--for instance, thus allowing any class anywhere in the class hierarchy to use this service.
jiangxuhong217 2005-02-02
  • 打赏
  • 举报
回复
接口是抽象类的特例,有抽象类的共性;
抽象类中可以有方法体的实现,但接口中所有的方法都是抽象的;
接口可以用来实现多重继承;
接口中的所有变量都默认是public static final类型的,而抽象类中的变量是自己根据类中自己设定的类型.
不徻写代码 2005-01-13
  • 打赏
  • 举报
回复
抽象类用于继承、扩展
不徻写代码 2005-01-13
  • 打赏
  • 举报
回复
类和接口的主要区别在类是建立在特定的属性上的一些操作和消息响应,接口则完全相反,它的操作更加独立些,并不是基于特定的属性来完成的。
因此则两者的使用区别主要表现在:
抽象类主要用于特定属性上操作或者消息响应的扩展。接口则完全基于某些共有操作扩展。
iforem 2005-01-13
  • 打赏
  • 举报
回复
//1)搜索论坛中相关的话题
//2)搜索google
//3)从字面上看,interface和abstract class的区别很简单,
// 如楼上所说的。但是要在程序中正确合理地使用interface
// 和abstract class,好像没有那么容易
//4)在java程序设计中,面向接口编程的思想很重要
vgvg 2005-01-13
  • 打赏
  • 举报
回复
抽象类
1》可以有实现的方法,也可以有没实现的方法,但你必须以abstract修饰。

借口
1》方法必须不可以实现,变量默认为static final public 类型的。

likeBean 2005-01-13
  • 打赏
  • 举报
回复
抽象类和接口都可以做多态使用,但是java中的abstract class和interface有很重要的区别,java中的class只能是单继承,就是说一个class只能extends 一个class,但是一个class可以同时implements多个interface,这是最重要的区别,其他的区别跟其他面向对象语言是一样的。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧