在实施接口中,我们利用interface语法,将interface从类定义中独立出来,构成一个主体。interface为类提供了接口规范。
在继承中,我们为了提高程序的可复用性,引入的继承机制。当时的继承是基于类的。interface接口同样可以继承,以拓展原interface。
![7ea19dd4bf6ef77e0504279f44c876b4.png](https://img-blog.csdnimg.cn/img_convert/7ea19dd4bf6ef77e0504279f44c876b4.png)
接口继承
接口继承(inheritance)与类继承很类似,就是以被继承的interface为基础,增添新增的接口方法原型。比如,我们以Cup作为原interface:
interface Cup { void addWater(int w); void drinkWater(int w); }
我们在继承Cup的基础上,定义一个新的有刻度的杯子的接口,MetricCup
![e31c10e311156f3b1a99a480e83d431e.png](https://img-blog.csdnimg.cn/img_convert/e31c10e311156f3b1a99a480e83d431e.png)
接口如下:
interface MetricCup extends Cup { int WaterContent(); }
我们增添了一个新的方法原型WaterContent(),这个方法返回一个整数(水量)。
i