chapter 10: inheritance 1. inheritance inheritance allows a software developer to derive a new...

17
Chapter 10: Inheritance 1

Upload: edmund-west

Post on 23-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Chapter 10: Inheritance

1

Page 2: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

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, or base class

The derived class is called the child class or subclass.

As the name implies, the child inherits characteristics of the parent

That is, the child class inherits the methods and data defined for the parent class

2

Page 3: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Inheritance

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 existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software

3

Page 4: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Inheritance

Inheritance relationships often are shown graphically in a UML class diagram, with an arrow with an open arrowhead pointing to the parent class

4

Inheritance should create an is-a relationship, meaning the child is a more

specific version of the parent

Person

Employee

Page 5: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Deriving Subclasses

In Java, we use the reserved word extends to establish an inheritance relationship

5

class Employee extends Person{ // class contents}

Page 6: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Inheritance is a fundamental Object Oriented concept

A class can be defined as a "subclass" of another class.The subclass inherits all data attributes of its superclassThe subclass inherits all methods of its superclassThe subclass inherits all associations of its superclass

The subclass can:Add new functionalityUse inherited functionalityOverride inherited functionality

Person- name: String

- dob: Date

Employee- employeeID: int

- salary: int- startDate: Date

superclass:

subclass:

6

Page 7: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Person- name: String

- dob: Date

Employee- employeeID: int

- salary: int- startDate: Date

public class Person{

private String name;private Date dob;

[...]

public class Employee extends Person{

private int employeID;private int salary;

private Date startDate;[...]

Employee anEmployee = new Employee();

7

Page 8: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Each Java class has one (and only one) superclass.

Inheritance creates a class hierarchyClasses higher in the hierarchy are more general and more abstractClasses lower in the hierarchy are more specific and concrete

Class

There is no limit to the number of subclasses a class can have

There is no limit to the depth of the class tree.

Class Class Class

Class

Class ClassClass

8

Page 9: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

The super Reference

Constructors are not inherited, even though they have public visibility

Yet we often want to use the parent's constructor to set up the "parent's part" of the object

The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor

9

Page 10: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

The super Reference

A child’s constructor is responsible for calling the parent’s constructor

The first line of a child’s constructor should use the super reference to call the parent’s constructor

The super reference can also be used to reference other variables and methods defined in the parent’s class

10

Page 11: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Constructors - Examplepublic class BankAccount{private String ownersName;private int accountNumber;private float balance;

public BankAccount(int anAccountNumber, String aName){

accountNumber = anAccountNumber;ownersName = aName;

}[...]

}

public class OverdraftAccount extends BankAccount{private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit){

super(anAccountNumber, aName);overdraftLimit = aLimit;

}}

11

Page 12: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

The protected Modifier

Visibility modifiers determine which class members are inherited 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

12

Page 13: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

The protected Modifier

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

However, protected visibility is not as tightly encapsulated as private visibility

Protected variables and methods can be shown with a # symbol preceding them in UML diagrams

13

Page 14: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

14Protected Visibility for Superclass Data

private data are not accessible to subclasses!

protected data fields accessible in subclasses

Page 15: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

UML Diagram for Words

Book

# pages : int

+ pageMessage() : void

Dictionary

- definitions : int+ definitionMessage() : void

15

Page 16: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Protected visibility

public class Person{private String name;protected String LastName;Public int age;}

public class Employee extends Person{

public void Setter (){

name = “Sara”; LastName = “Ali”; Age = 20;}

Employee anEmployee = new Employee();System.out.print(anEmplyee.name);System.out.print(anEmplyee.LastName);System.out.print(anEmplyee.age);

16

Page 17: Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called

Multiple Inheritance

Java supports single inheritance, meaning that a derived class can have only one parent class

Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents

Java does not support multiple inheritance

17