Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接口和抽象类的区别是什么 #146

Closed
raoyh opened this issue Feb 17, 2019 · 8 comments
Closed

接口和抽象类的区别是什么 #146

raoyh opened this issue Feb 17, 2019 · 8 comments
Labels
enhancement New feature or request

Comments

@raoyh
Copy link

@raoyh raoyh commented Feb 17, 2019

在JDK8中,接口也可以定义静态方法,可以直接用接口名调用。实现类和实现是不可以调用的。
如果同时实现两个接口,接口中定义了一样的默认方法,必须重写,不然会报错

@Snailclimb
Copy link
Owner

@Snailclimb Snailclimb commented Feb 21, 2019

感谢您的回答!

@Snailclimb Snailclimb added the enhancement New feature or request label Mar 3, 2019
@lanmeiniu
Copy link

@lanmeiniu lanmeiniu commented May 31, 2019

关于抽象类
JDK 1.8以前,抽象类的方法默认访问权限为protected
JDK 1.8时,抽象类的方法默认访问权限变为default
关于接口
JDK 1.8以前,接口中的方法必须是public的
JDK 1.8时,接口中的方法可以是public的,也可以是default的
JDK 1.9时,接口中的方法可以是private的

@powerLeePlus
Copy link

@powerLeePlus powerLeePlus commented Sep 17, 2019

jdk1.8后,接口方法可以有方法体,需要用default或static修饰。实现类无需重新。当然default方法重写也是可以的。如果一个类实现多个接口,多个接口有相同default方法,则子类必须重写该方法

@WangZhen-Ddm
Copy link

@WangZhen-Ddm WangZhen-Ddm commented Apr 26, 2020

请问,在jdk1.8中
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap继承了AbstractMap这一抽象类并实现了Map<>接口,为何其中的代码都未加@OverRide进行重写

@wangpeipei90
Copy link

@wangpeipei90 wangpeipei90 commented Apr 27, 2020

@OverRide 是annotation,貌似不是必须的

@chengweng
Copy link

@chengweng chengweng commented Apr 27, 2020

请问,在jdk1.8中
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap继承了AbstractMap这一抽象类并实现了Map<>接口,为何其中的代码都未加@OverRide进行重写

看下@OverRide它的源码
图片
是SOURCE级别

@WangZhen-Ddm
Copy link

@WangZhen-Ddm WangZhen-Ddm commented Apr 27, 2020

请问,在jdk1.8中
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap继承了AbstractMap这一抽象类并实现了Map<>接口,为何其中的代码都未加@OverRide进行重写

看下@OverRide它的源码
图片
是SOURCE级别

source级别的注解可以选择不加吗,class和runtime级别的注解是必须的吗

@Amuao
Copy link

@Amuao Amuao commented Oct 21, 2020

请问,在jdk1.8中
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap继承了AbstractMap这一抽象类并实现了Map<>接口,为何其中的代码都未加@OverRide进行重写

看下@OverRide它的源码
图片
是SOURCE级别

source级别的注解可以选择不加吗,class和runtime级别的注解是必须的吗

java的注解出现在Jdk1.5,而集合框架早在Jdk1.2就出现了,我觉得就是懒得加 专门去加这玩意干啥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants