java 虚类_Java虚类与接口

As you already know, a class in Java can extend only one other class.  However, a Java class can implementany 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值