版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Inheritance,2,Inheritance,Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass The derived class is called the child class or subclass. Creates an is-a relationship The subclass is a more specific version of th
2、e Original (Remember has-a is aggregation.),Inheritance,The child class inherits the methods and data defined for the parent class To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones Software reuse is at the heart of inheritance By using exist
3、ing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software,4,Deriving Subclasses,In Java, we use the reserved word extends to establish an inheritance relationship,class Dictionary extends Book / class c
4、ontents ,Dictionary webster = new Dictionary(); webster.message(); webster.defMessage();,public class Book protected int pages = 1500; public String message() System.out.println(“Number of pages: ” + pages); ,public class Dictionary extends Book private int definitions = 52500; public void defMessag
5、e() System.out.println(“Number of definitions” + definitions); System.out.println(“Definitions per page: ” + (definitions/pages); ,Number of pages: 1500,Number of definitions: 52500,Definitions per page: 35,Some Inheritance Details,An instance of a child class does not rely on an instance of a paren
6、t class Hence we could create a Dictionary object without having to create a Book object first Inheritance is a one-way street The Book class cannot use variables or methods declared explicitly in the Dictionary class,7,The protected Modifier,Visibility modifiers determine which class members are in
7、herited and which are not Variables and methods declared with public visibility are inherited; those with private visibility are not But public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected,8,The protected Modifi
8、er,The protected modifier allows a member of a base class to be inherited into a child Protected visibility provides more encapsulation than public visibility does the best possible encapsulation that permits inheritance,9,The super Reference,Constructors are not inherited, even though they have pub
9、lic visibility Yet we often want to use the parents constructor to set up the parents part of the object The super reference can be used to refer to the parent class, and often is used to invoke the parents constructor,The super Reference,A childs constructor is responsible for calling the parents c
10、onstructor The first line of a childs constructor should use the super reference to call the parents constructor The super reference can also be used to reference other variables and methods defined in the parents class,public class Book protected int pages; Book(int numPages) pages = numPages; ,pub
11、lic class Dictionary private int definitions; Dictionary(int numPages, int numDefinitions) super(numPages); definitions = numDefinitions; ,Multiple Inheritance,Java supports single inheritance, meaning that a derived class can have only one parent class Multiple inheritance allows a class to be deri
12、ved from two or more classes, inheriting the members of all parents Collisions, such as the same variable name in two parents, have to be resolved Java does not support multiple inheritance In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead (will discu
13、ss later),13,Overriding Methods,When a child class defines a method with the same name and signature as a method in the parent class, we say that the childs version overrides the parents version in favor of its own. Signature: methods name along with number, type, and order of its parameters The new
14、 method must have the same signature as the parents method, but can have a different body The type of the object executing the method determines which version of the method is invoked,Overriding,A parent method can be invoked explicitly using the super reference If a method is declared with the fina
15、l modifier, it cannot be overridden The concept of overriding can be applied to data and is called shadowing variables Shadowing variables should be avoided because it tends to cause unnecessarily confusing code,public class Book protected int pages; Book(int numPages) pages = numPages; public void
16、message() System.out.println(“Number of pages: ” + pages); ,public class Dictionary protected int definitions; Dictionary(int numPages, int numDefinitions) super(numPages); definitions = numDefinitions; public void message() System.out.println(“Number of definitions” + definitions); System.out.print
17、ln(“Definitions per page: ” + (definitions/pages); super.message(); ,16,Overloading vs. Overriding,Dont confuse the concepts of overloading and overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, on
18、e in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different data Overriding lets you define a similar operation in different ways for different object types,17,Class Hierarchies,A child class of one parent can be the parent of another child, forming a class hierarchy,18,Class Hierarchies,Two children of the same parent are called siblings However they are not related by inheritance because one is not used to derive another. Common features sho
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【期中专项复习卷】部编版六年级上册语文:9.非连续性文本阅读(含答案)
- 2026及未来5年中国三层网带式散热器市场现状数据分析及前景预测报告
- 2026及未来5年中国SPA固定式龙门洗车机市场现状数据分析及前景预测报告
- 2026及未来5年中国IGBT模块市场现状数据分析及前景预测报告
- 2026年经济法基础初级会计考试真题及答案
- 2026年国企合规风控业务考试题库及答案
- 2026年电大开学测评模拟试题(含答案)
- 大名县得祥清真食品有限公司介绍企业发展分析报告模板
- 2026年安徽文科模拟试卷模拟试题及答案详解
- 2026年广西路政模拟试题(含答案)
- 2026年食品安全员专业知识考试题库(含答案)
- 40248-2026人员密集场所消防安全管理
- 2025年湖南能源集团招聘考试笔试试题含答案
- 数字测绘成果质量保证体系与管控方案 技术标专用
- 半导体供应链韧性研究与对策分析
- 医学康复科病历书写规范培训
- 2026年秋季小学低年级班主任课堂纪律管理经验课件
- 2026年小学青岛版(六三制)六年级科学上册全册教案
- 2026年秋季开学初三弱科突破备考规划课件
- 围墙及厂区道路改造工程施工组织设计
- 2026年长期照护师资格考试试卷及答案(共十二套)
评论
0/150
提交评论