Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2066349
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: Java

2010-01-14 14:41:54

As you already know, a class in Java can extend only one other class.  However, a Java class can implement any number of interfaces. (Multiple inheritance is allowed with interfaces.)

Similar to an abstract class

An interface is similar, but not identical, to an abstract class.

(An abstract class may contain abstract methods or concrete methods, or a combination of the two.  However, an abstract class cannot be instantiated.  An interface also cannot be instantiated.)
For example, all methods declared in an interface must be (are implicitly) abstract, but that is not true for an abstract class.  An abstract class can also contain fully-defined (concrete) methods.

A totally abstract class

At the risk of offending the purists, I will coin a new term here and say that an interface is similar to a totally abstract class (one which contains only abstract method declarations and final variables).

The cardinal rule

The cardinal rule in implementing interfaces is:

If a class implements an interface, it must provide a concrete definition for all the methods declared by that interface, and all the methods inherited by that interface.  Otherwise, the class must be declared abstract and the definitions must be provided by a class that extends the abstract class.
阅读(4160) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~