文档章节

关于Java inplement与extends 区别

Oscarfff
 Oscarfff
发布于 2017/02/10 15:34
字数 478
阅读 54
收藏 0

http://www.cs.pomona.edu/~kim/CSC051F13/Lectures/Lecture28/Lecture28_7.html

Difference between extends and implements

  • When a subclass extends a class, it allows the subclass to inherit (reuse) and override code defined in the supertype.
  • When a class implements an interface, it allows an object created from the class to be used in any context that expects a value of the interface.

Thus if a variable has a type given by an interface, then any object from a class implementing that interface can be assigned to that variable. If a formal parameter's type is given by an interface, any object from a class implementing the interface can be used as an actual parameter in that slot. While we haven't seen an example here, we may also use a superclass in these ways. For example, a variable of type LaundryImpl can be assigned an object of type Pants or Tshirt.

Since extends is more powerful, why use implements? Java requires that a class may extend at most one other class. Extending multiple classes introduces a very difficult problem if, for example, the two classes that we are trying to extend both define an instance variable with the same name or a method with the same signature. How can our new class inherit both since they have the same name? Numerous languages have tried to define rules to deal with this, but none of the solutions has been good. Java avoids it by disallowing this multiple inheritance. So if we want a class to be a subtype of more than one other type, only one may be a class that we extend, the others must be interfaces that we implement.

Initially, that may seem unreasonable and restrictive, but there are lots of uses for implementing types without inheriting code. We have seen this with GUI listeners:

public class BallController extends WindowController 
                                  implements ActionListener, 
                                             AdjustmentListener

This means that our BallController can be used anywhere our program expects a WindowController, an ActionListener, an AdjustmentListener, or any supertype of these. It only inherits code from WindowController. Why?ItemListener, ActionListener, and AdjustmentListener do not provide any code to inherit. They simply set up types that we can implement. By implementing a type, we promise to define the methods for that interface. As a result, Java knows it can treat the new class as an instance of that type because the promised method must be supplied for the program to compile.

In this course, you will not use inheritance much in your own code aside from writing classes that extend Controller, WindowController, or ActiveObject, but it will be helpful to understand what is happening in those cases.

To summarize:

  • Both implements and extends create subtypes
  • extends inherits code from the supertype (whether class or interface)
  • implements sets up a promise to implement the methods declared in the supertype

© 著作权归作者所有

Oscarfff
粉丝 73
博文 816
码字总数 97116
作品 0
崇明
后端工程师
私信 提问
提给程序员和开发者的 10 道 Java 泛型面试题

关于泛型的面试题在 Java面试中变得越来越常见,因为 Java 5问世已经有相当长的时间了,越来越多的应用已经迁移到Java 5上来了,并且几乎所有新的Java开发工作也都是在Tiger(Java 5的项目代号...

lwei
2013/10/18
13.6K
30
10 道关于 Java 泛型的面试题

1.Java中的泛型是什么 ? 使用泛型的好处是什么? 这是在各种Java泛型面试中,一开场你就会被问到的问题中的一个,主要集中在初级和中级面试中。那些拥有Java1.4或更早版本的开发背景的人都知道...

蚂蚁-Declan
2018/10/24
169
0
Kotlin 范型之协变、逆变

一. 类(Class) 与类型(Type) Kotlin 中类和类型是不一样的概念。 下图充分展示了它们的区别。 二. 型变 型变是指转换后的继承关系。 Kotlin 的型变分为逆变、协变和不变。 2.1 协变 如果 A ...

fengzhizi715
06/24
0
0
15个顶级Java多线程面试题及回答

Java 线程面试问题 在任何Java面试当中多线程和并发方面的问题都是必不可少的一部分。如果你想获得任何股票投资银行的前台资讯职位,那么你应该准备很多关于多线程的问题。在投资银行业务中多...

LCZ777
2014/05/27
645
0
java中高级大公司多线程面试题

1)在Java中Lock接口比synchronized块的优势是什么?你需要实现一个高效的缓存,它允许多个用户读,但只允许一个用户写,以此来保持它的完整性,你会怎样去实现它? lock接口在多线程和并发编...

java成功之路
2018/10/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

秒杀系统思路

业务分析 技术挑战 请求响应要快:无论成功失败,需要尽快返回给用户 架构设计   前端:静态化   站点层:限制请求数   服务层:乐观锁写缓存   数据库CAP:读写高可用,一致性,扩容...

雷开你的门
17分钟前
7
0
最全的教育行业大数据解决方案,个个针对痛点

大数据的悄然兴起也带动了教育行业的革新,移动教育、云课堂等的出现,使得教育行业再次成为了可以中长期保持高景气的行业。然而,初涉数据领域的教育行业同时也面临着相当大的难题,还需要更...

朕想上头条
21分钟前
5
0
预约模块设计分析

1.预约功能描述: 预约是小程序中常见的一种商品管理系统,商家可根据商品或服务的特性,灵活设置预约细节,为用户提供线上预约服务,如场地预约,商品预定等,实现高效经营。 预约场景: ...

鱼煎
25分钟前
4
0
阿里云日志服务构建网站实时分析大盘实战

场景分析 挖掘数据价值是当前企业级网站共同面临的问题。买买网是一个电商平台网站,每天拥有大量的用户访问和购买记录。为了引导用户直接消费,提升购买率和转化率,不同的用户类别需要推荐...

阿里云官方博客
26分钟前
2
0
TL665xF-EasyEVM开发板硬件处理器、NAND FLASH、RAM

广州创龙结合TI KeyStone系列多核架构TMS320C665x及Xilinx Artix-7系列FPGA设计的TL665xF-EasyEVM开发板是一款DSP+FPGA高速大数据采集处理平台,其底板采用沉金无铅工艺的6层板设计,适用于高...

Tronlong创龙
29分钟前
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部