inheritance csc 171 fall 2004 lecture 18. reading read horstmann, chapter 11
TRANSCRIPT
![Page 1: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/1.jpg)
InheritanceInheritance
CSC 171 FALL 2004
LECTURE 18
![Page 2: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/2.jpg)
READINGREADING
Read Horstmann, Chapter 11
![Page 3: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/7.jpg)
![Page 8: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/9.jpg)
![Page 10: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/14.jpg)
![Page 15: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/15.jpg)
![Page 16: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/17.jpg)
BankAccountBankAccount
Instance variable “balance”Methods “deposit” and “withdraw”
![Page 18: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/19.jpg)
SavingsAccountSavingsAccount
A bank account with an interest rate
![Page 20: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/33.jpg)
public double getPay() {return annualSalary / 12;
}}
![Page 34: Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022062321/56649ee45503460f94bf3b7d/html5/thumbnails/38.jpg)
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.