Chapter 5 Inheritance

1. Classes, Superclasses, and Subclasses

2. Objects: The Cosmic Superclass

3. Generic Array Lists

4. Objects wrappers and Autoboxing

5. Methods with a Variable Number of Parameters

6. Enumeration Classes

7. Reflection

8. Design Hints for Inheritance

  The idea behind inheritance is that you can create new classes that are built on existing classes. When you inherit from an existing class, you reuse

(or inherit) its methods, and you can add new methods and fields to adapt your new class to new situations.

1. Classes, Superclasses, and Subclasses

  Every manager is an employee

  Here is how you define a Manager class that inherits from the Employee class

   class Manager extends Employee { add methods and fields }

  The keyword extends indicates that you are making a new class that derives from an existing class.

  The existing class is called the superclass, base class, or parent class.

The new class is called the subclass, derived class, or child class.

  When defining a subclass by extending its superclass, you only need to indicate the differences between the subclass and the superclass. When

designing classes, you place the most general methods into the superclass and more specialized methods in its subclasses. Factoring out common

functionality by moving it to a superclass is common in object-oriented programming.

  However, some of the superclass methods are not appropriate for the Manager subclass. In particular, the getSalary method should return the sum

of the base salary and bonus. You need to supply a new method to override the superclass method:

   public double getSalary() { return salary + bonus; // won‘t work }

  However, that won’t work. The getSalary method of the Manager class has no direct access to the private fields of the superclass.

   public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + bonus; }

  We need to indicate that we want to call the getSalary method of the Employee super-class, not the current class. You use the special keyword

super for this purpose. The call super.getSalary() calls the getSalary method of the Employee class

  Finally, let us supply a constructor 

1 public Manager(String n, double s, int year, int month, int day)
2 {
3     super(n, s, year, month, day);
4     bonus = 0;
5 }

  Here, the keyword super has a different meaning. The instruction super(n, s, year, month, day); is shorthand for “call the constructor of the

Employee superclass with n, s, year, month, and day as parameters.”

  The call using super must be the first statement in the constructor for the subclass.

  P194

 1 //Employee.java
 2 import java.util.*;
 3
 4 public class ManagerTest{
 5     public static void main(String[] args) {
 6
 7         Manager boss  = new Manager("Boss",50000,1987,12,1);
 8         boss.setBonus(5000);
 9
10         Employee[] staff = new Employee[3];
11         staff[0] = boss;
12         staff[1] = new Employee("lisi",30000,1989,10,1);
13         staff[2] = new Employee("wangwu",20000,1990,3,12);
14
15         // print out information about all Employee objects
16         for (Employee e : staff)
17             System.out.println("name=" + e.getName() +
18                                ",salary=" + e.getSalary() +
19                                ",hireDay=" + e.getHireDay());
20     }
21 }
 1 //Manager.java
 2 public class Manager extends Employee{
 3
 4     private double bonus;
 5
 6     public Manager(String n, double s, int year, int month, int day){
 7         super(n, s, year, month, day);
 8         bonus = 0;
 9     }
10
11     public double getSalary()
12     {
13          double baseSalary = super.getSalary();
14         return baseSalary + bonus;
15      }
16
17      public void setBonus(double b)
18      {
19          bonus = b;
20      }
21 }
 1 //ManagerTest.java
 2
 3 import java.util.*;
 4
 5 public class ManagerTest{
 6     public static void main(String[] args) {
 7
 8         Manager boss  = new Manager("Boss",50000,1987,12,1);
 9         boss.setBonus(5000);
10
11         Employee[] staff = new Employee[3];
12         staff[0] = boss;
13         staff[1] = new Employee("lisi",30000,1989,10,1);
14         staff[2] = new Employee("wangwu",20000,1990,3,12);
15
16         // print out information about all Employee objects
17         for (Employee e : staff)
18             System.out.println("name=" + e.getName() +
19                                ",salary=" + e.getSalary() +
20                                ",hireDay=" + e.getHireDay());
21     }
22 }

  

  

  

  

时间: 2024-10-07 14:07:18

Chapter 5 Inheritance的相关文章

JavaScript- The Good Parts Chapter 5 Inheritance

Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but confusion. . .—William Shakespeare, The Tragedy of King Richard the Second Inheritance is an important topic in most programming languages. In the cl

NHibernate官方文档中文版--基础ORM(Basic O/R Mapping)

映射声明 对象/关系映射在XML文件中配置.mapping文件这样设计是为了使它可读性强并且可修改.mapping语言是以对象为中心,意味着mapping是围绕着持久化类声明来建立的,而不是围绕数据表. 要注意的是,尽管很多NHibernate使用者选择手动定义XML文件,但是仍然有很多工具可以用来生成mapping文件,包括NHibernate.Mapping.Attributes 库和各种各样基于模板的代码生成工具(CodeSmith, MyGeneration). 让我们用一个mappin

《Java核心技术 卷1 基础知识 原书第9版》pdf

下载地址:网盘下载 内容简介 编辑 CayS.Horstmann等编著,公飞编译的<Java核心技术>(CoreJava)自第1版出版以来,一直备受广大Java程序设计人员的青睐,畅销不衰,是Java经典书籍.第8版针对JavaSE6平台进行了全面更新,囊括了Java平台标准版(JavaSE/J2SE)的全部基础知识,提供了大量完整且具有实际意义的应用实例,详细介绍了Java语言基础知识.面向对象程序设计.接口与内部类.事件监听器模型.swing图形用户界面程序设计.打包应用程序.异常处理.登

高级数据结构

数据结构清单 Binomial Heap Leftist Tree:左倾堆 ? 重型网络教程 1.纸上谈兵:算法与数据结构 2.CS 598 JGE:Advanced Data Structures(Fall 2015) 3.COP 5536 Advanced Data Structures(Florida) 4.wikibooks Data Structures 5.Geeksforgeeks(very much) 6.COMP 5408:Advanced Data Struestures 7

C++学习书籍推荐《Effective C++ 第三版(英文)》下载

百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing training and consulting services to clients worldwide. He is the author of the best-selling Effective C++ series of books (Effective C++, More Effective C+

Chapter 17. Objects and Inheritance(对象与继承)

SELECT DISTINCT fa.application_short_name 模块,                 b.responsibility_name 职责名称, fa.application_name 应用产品,                 b.responsibility_key 责任关键字, b.description 说明,                 DECODE (b.data_group_id, 0, '标准', '') 数据组,              

Cpp Chapter 13: Class Inheritance Part1

class inheritance lets you derive new classes from old ones, inheriting its properties of the old class, called the base class With inheritance, you can: 1 add functionality to existing classes 2 add the data a class represents 3 modify how a class m

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures things about 1. Declare an enumeration type. 2. Create and use an enumeration type. 3. Declare a structure type. 4. Create and use a structure type. 5. Explain

advanced dom scripting dynamic web design techniques Chapter 2 CREATING YOUR OWN REUSABLE OBJECTS

JavaScript is all about objects. Objects are the foundation of everything, so if you’re unfamiliar with objects, you’re going to learn quickly. The goal of this book is not to be a JavaScript or DOM code reference, but in order to make sure you under