inheritance csc 171 fall 2004 lecture 18. reading read horstmann, chapter 11

38
Inheritance Inheritance CSC 171 FALL 2004 LECTURE 18

Upload: ginger-mccoy

Post on 02-Jan-2016

223 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

InheritanceInheritance

CSC 171 FALL 2004

LECTURE 18

Page 2: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

READINGREADING

Read Horstmann, Chapter 11

Page 3: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Design MethodologyDesign Methodology

1. Problem Definition

2. Requirements Analysis

3. Architecture

4. Construction

5. Testing

6. Future Improvements

Page 4: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Classes and ObjectsClasses and ObjectsObject oriented programs

– Define classes of objects– Make specific object out of class definitions– Run by having the objects interact

A class is a type of thing– Instructor

An object is a specific thing– Ted

An object is an instance of a class

Page 5: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

HierarchiesHierarchies

Humans have found that organizing concepts into hierarchies a useful method of organizing information

Page 6: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

HIERARCHIESHIERARCHIESHierarchies are nested groupings.

Examples of hierarchies Levels of organization in our bodies:

– Organism: Organ Systems: Organs: Tissues: Cells: Organelles Ecology:

– Biome: Community: Population: Organism Political boundaries:

– USA: New York State: Monroe County: City of Rochester

Notice how each group is completely subordinate to any group on its left.

Page 7: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11
Page 8: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Inheritance HierarchiesInheritance HierarchiesObject oriented languages, such as JAVA

allows us to group classes into inheritance hierarchies.

The most general classes are near the root– superclasses

The more specific classes are near the leaves– Subclasses

Subclasses inherit attributes from superclasses

Page 9: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11
Page 10: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Example: Banking SystemsExample: Banking Systems

Consider a system that supports Savings and Checking accounts– What are the similarities?– What are the specifics

Page 11: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

AccountsAccounts

Both savings and Checking accounts support the idea of– Balance– Deposit– Withdraw

Savings accounts pay interest checking accounts do not

Checking accounts have transaction fees, savings accounts do not

Page 12: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Super & Sub classesSuper & Sub classesMore general concepts are in super classesMore specific concepts are in sub classesSub classes extend (inherit from)

superclasses

Page 13: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Why inheritance?Why inheritance?

The power of inheritance is that sub-classes inherit the capabilities of the super-classes they extend

This reduces code redundancy

Page 14: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11
Page 15: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11
Page 16: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Example: Banking systemsExample: Banking systems

Bank accounts are a type of object Savings accounts are a type of bank account Checking Accounts are bank accounts

public class BankAccount { . . . }public class SavingsAccount extends BankAccount { . . .}public class CheckingAccount extends BankAccount { . . .}

Page 17: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

BankAccountBankAccount

Instance variable “balance”Methods “deposit” and “withdraw”

Page 18: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public class BankAccount { public void deposit (double amount) { balance += amount; }

public void withdraw(double amount) { if (amount <= balance)

balance -= amount; }

private double balance; }

Page 19: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

SavingsAccountSavingsAccount

A bank account with an interest rate

Page 20: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public class SavingsAccount extends BankAccount { public SavingsAccount(double rate) { interestRate = rate; }

public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); }

private double interestRate; }

Page 21: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Inheritance and MethodsInheritance and Methods

Override method: Supply a different implementation of a method that exists in the superclass

Inherit method: Don't supply a new implementation of a method that exists in the superclass

Add method: Supply a new method that doesn't exist in the superclass

Page 22: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Inheritance and FieldsInheritance and Fields

Inherit field: All fields from the superclass are automatically inherited

Add field: Supply a new field that doesn't exist in the superclass

Can't override fields

Page 23: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Checking AccountChecking Account

A Bank Account with transaction feesNeed to keep tack of transactions

Page 24: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Checking AccountChecking Account

public class CheckingAccount extends BankAccount { private int transactionCount;

public CheckingAccount() { transactionCount = 0; }

}

Page 25: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public void deposit(double amount) {   transactionCount++;   super.deposit(amount);}

public void withdraw(double amount) {   transactionCount++;   super.deposit(amount);}

Page 26: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

ExcerciseExcercise

Define a base class “Employee”– Employees have names– Define constructor

Page 27: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public class Employee {private String name;

public Employee(String name) {this.name = name;

}public String name() { return name;}

}

Page 28: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

ExcerciseExcercise

Define a sub class “HourlyEmployee”– Names, hourly rates, and hours worked– Define constructor, clockhours and “getPay()”

Page 29: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public class HourlyEmployee extends Employee {private double wage, hoursworked;

public HourlyEmployee(String name, double wage) { super(name);this.wage = wage;hoursworked = 0;

}

Page 30: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public void clockHours(double hours) {hoursworked += hours;

}public double getPay() {

double pay = hoursworked * wage;hoursworked = 0 ;return pay;

}}

Page 31: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

ExcerciseExcercise

Define a sub-class “SalaryEmployee”– Names and annual Salary– Define constructor, and “getPay()” (montly)

Page 32: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public class SalaryEmployee extends Employee {private double annualSalary;

public SalaryEmployee(String name, double salary) { super(name);annualSalary = salary;

}

Page 33: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

public double getPay() {return annualSalary / 12;

}}

Page 34: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

TERMINOLOGYTERMINOLOGY

A base class is often called a parent class. A sub-class is then called a child class (or derived class)

Parents of parents are called ancestor classes

Children of children are called descendent classes

Page 35: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Overriding vs. OverloadingOverriding vs. Overloading

Overloading refers to having a different configuration of parameters for the same method name

Overriding refers to the redefinition of a method in a subclass

Page 36: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

ACCESS ISSUESACCESS ISSUES

private instance variables or methods in a super (base,parent) class are not accessible in sub (derived, child) classes.

So, private methods and variable are effectively not inherited.– They are “there” but you have to use the super-

class accessor and mutator methods.

Page 37: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

ProtectedProtected

protected methods and variables can be accessed inside derived classes, or in any class in the same package.

Page 38: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11

Package AccessPackage Access

If you don not place any of public, private, or protected before an instance variable or method definition, then it will have package access (also know as default access or friendly access). It will be visible to any class in the same package, but not outside.