2002 prentice hall. all rights reserved. 1 introduction to inheritance inheritance: –1 of 3 main...

16
2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance • Inheritance: 1 of 3 main features of OOP Form of software reusability (Derived) classes are created by absorbing the methods and variables of an existing (base) class It then adds its own methods to enhance its capabilities “Is a” relationship: derived class object can be treated as base class object “Has a” relationship: class object has object references as members A derived class can only access non-private base class members unless it inherits accessor functions Constructors are not inherited

Upload: junior-morrison

Post on 01-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

1

Introduction to Inheritance

• Inheritance:– 1 of 3 main features of OOP

– Form of software reusability

– (Derived) classes are created by absorbing the methods and variables of an existing (base) class

– It then adds its own methods to enhance its capabilities

– “Is a” relationship: derived class object can be treated as base class object

– “Has a” relationship: class object has object references as members

– A derived class can only access non-private base class members unless it inherits accessor functions

– Constructors are not inherited

Page 2: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

2

Base Classes and Derived Classes

Base class Derived classes Student GraduateStudent

UndergraduateStudent Shape Circle

Triangle Rectangle

Account CheckingAccount SavingsAccount

To specify that class Two is derived from class One

class Two extends One or class Circle extends Shape

{ {

Page 3: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

3

Members

• All inheritance in Java is public inheritance– Can be accessed by base class or any class derived from that

base class

– No analog to C++ features of private and protected

• Definitions– super, base, parent class

– child, sub-, derived class

Page 4: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

4

Base Classes and Derived Classes

Inheritance forms a tree-like hierarchyInheritance hierarchy for university CommunityMembers.

CommunityMember

Employee Student Alumnus

Faculty Staff

Administrator Teacher

Page 5: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

5

Relationship between Base Classes and Derived Classes

• Text’s Example: – class Employee (parent class)

– class Manager (child class)

Page 6: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall.All rights reserved.

Outline6

class Employee{ public Employee(String n, double s, int year, int month, int day) { … }

public String getName() { … }

public double getSalary() { … }

public Date getHireDay() { … }

public void raiseSalary(double byPercent) { … }

private String name; private double salary; private Date hireDay;}

Page 7: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall.All rights reserved.

Outline7

class Manager extends Employee { //added methods here //redefined methods here private double bonus; }

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

public double getSalary() { double baseSalary = getSalary(); //still won’t work return baseSalary + bonus; }

Manager class has no direct access to private fields of superclass

Calls itself – infinite recursion

Page 8: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall.All rights reserved.

Outline8

class Manager extends Employee{ /** @param n the employee's name @param s the salary @param year the hire year @param month the hire month @param day the hire day */ public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; }

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

public void setBonus(double b) { bonus = b; }

private double bonus;}

Manager inherits from Employee

Calls the constructor for the employee class

Calls the getSalary method for the employee class

Page 9: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall.All rights reserved.

Outline9

// construct a Manager object Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); boss.setBonus(5000);

Employee[] staff = new Employee[3];

// fill the staff array with Manager and Employee objects staff[0] = boss; staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

// print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());

Create a new manager and set bonus

Create an array of parent class Employee

Fill array with employees and manager

foreach loop

Which getSalary( ) will it call?

Page 10: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall.All rights reserved.

Outline10

Carl’s salary is 80000 + 5000 = 85000

Page 11: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

11

Polymorphism

• The fact that an object variable (such as e) can refer to multiple actual types is called polymorphism.

• Automatically selecting the appropriate methods at run time is called dynamic binding.

• Note: In Java, you do not need to declare a method as virtual. Dynamic binding is the default behavior.

• If you do not want a method to be virtual, you tag it as final.

Page 12: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

12

Preventing Inheritance: Final Methods

class Employee{ … public final String getName( ) { return name; } …}

Page 13: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

13

Inheritance

• Inheritance does not have to stop at one layer of classes.

• Could have an Executive class that extends Manager.

• Path from a particular class to its ancestor is the inheritance chain.

• May be more than one chain of descent. For example, a subclass Programmer and Secretary that extends Employee.

• Java does not support multiple inheritance. (Has interface classes, though.)

Page 14: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

14

Polymorphism

• A simple rule for inheritance ‘is-a’ rule.– subclass ‘is-an’ object of the superclass

– every manager ‘is-an’ employee

• The opposite is not true.• Another way of formulating rule is substitution

principle.– You can use a subclass object whenever the program expects a

superclass object.

– Already did this in last example.

Page 15: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

15

Polymorphism

Last example.– staff[0] and boss refer to the same object.– staff[0] is only considered to be an Employee object by

compiler.

– That means you can call

boss.setBonus (5000); //ok

– but you can’t call

staff[0].setBonus(5000); //error

Page 16: 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

2002 Prentice Hall. All rights reserved.

16

Preventing Inheritance: Final Classes

final class Executive extends Manager{ …}