栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Interface

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Interface

Only interfaces can do multiple inheritance

extends and implements

• A class can only extend 1 class: – meaning 1 class can only have 1 parent; – a PinkBunnyRabbit can only have one direct parent – Rabbit.

• A class can implement as many interfaces as it likes! – A PinkBunnyRabbit can be (via interfaces) a BigBadies and Teleportable.

接口中的所有东西都是抽象的(public abstract void run(){})

不能在接口中实例化出一个对象

Notes on Abstruct class and interface

Neither abstract classes and interfaces can have an instance made of them.

• If you don’t provide any method implementation, then use an interface instead of an abstract class.

• A class can implement many interfaces, but extends only one superclass.

• Interfaces are how Java provides (a kind of) multiple inheritance.

• If even one method in a class is declared to be abstract, then the whole class must be declared abstract.

• Both abstract classes and interfaces can contain constants, which will be inherited by classes that extend or implement them, respectively.

An interface may have many methods. If a class implements an interface, but only implements some of its methods, then this class becomes an abstract class; it cannot be instantiated.

Like classes, interfaces can be extended as well

Same named methods: • If they have different parameters, then Child interface has both (this is same as overloading). • If they differ by only return type, then error. • If the two methods are identical, only keep one. – Same named constants: we keep both constants. To refer to them, use parent interface name as prefix.

default methods  Allow developers to add new functionality to interfaces, without impacting any existing classes that are already implementing the interface. • Can be overridden in the class that implements the interface. • Provide backward compatibility for existing interfaces.

– static methods  Allow developers to define utility methods in the interface. • Are similar to default methods, but cannot be overridden in the class that implements the interface.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号