CPGM21X1 - Sheridan College

Download Report

Transcript CPGM21X1 - Sheridan College

INFO 16029
Problem Solving and
Programming Logic
Inheritance
What is Inheritance?
A type of relationship between
classes.
There are a few different kinds of class
relationships
Inheritance is also called
Generalization
Occurs when one specific class
“inherits” the characteristics and
behaviors of a more general class
It’s easier to understand with
examples 
7/18/2015
Wendi Jollymore, ACES
2
What is Inheritance?
Which of these are valid/true
assertions?
All cars are vehicles
All airplanes are vehicles
All vehicles are cars
True
False
We can say that:
A car is a type of vehicle
A bike is a type of vehicle
A UFO is a type of vehicle
7/18/2015
Wendi Jollymore, ACES
3
What is Inheritance?
We often say that Inheritance is an
“is a” relationship
A
A
A
A
car is a vehicle
bike is a vehicle
fork is a utensil
cat is a feline
One term is more general, the other
more specific
Vehicle, Utensil, Feline are general
Car, bike, fork, cat, are specific
7/18/2015
Wendi Jollymore, ACES
4
What is Inheritance?
What characteristics and behaviors
to all vehicles share?
Characteristics:
Colour, number of wheels, number of
passengers, brand or model
Behaviors:
Start, stop, accelerate/decelerate
7/18/2015
Wendi Jollymore, ACES
5
What is Inheritance?
What characteristics/behaviors are
different between a car and an
airplane?
Airplanes can fly
Cars can reverse
7/18/2015
Wendi Jollymore, ACES
6
What is Inheritance
In programming, the more general
class is called the parent class or
super class
The more specific classes are called
child classes or sub classes.
In Java, a child class
is only allowed one
parent class…
7/18/2015
Parent
Class
…however, a parent
can have many child
classes
Child
Child
Child
Class
Class
Class
Wendi Jollymore, ACES
7
What is Inheritance
Examples
Utensils
Fork
Spoon
Knife
Vehicles
Car
7/18/2015
Airplane
Bicycle
Wendi Jollymore, ACES
8
What is Inheritance?
A child class contains the
characteristics and behaviors of its
parent
It might also have characteristics and
behaviors of its own
It might also have the same behaviors
as the parent, but they just work
differently
Example: You can drive a car and drive
a plane, but not the same way
7/18/2015
Wendi Jollymore, ACES
9
Example
Full time employees
work 40 hours per week
get paid an annual salary
weekly pay = annual salary / number
of full weeks they work
Part time employees
can work a varying number of hours
per week
weekly pay = #of hours worked *
hourly rate.
7/18/2015
Wendi Jollymore, ACES
10
Example
Sales employees
have varying hours
weekly pay = commission amount = a
percentage of their weekly sales
they also get a flat lump sum or base
pay per week
Temporary sales employees
just like regular sales employees
except that they don't get the base
pay each week, just commissions
7/18/2015
Wendi Jollymore, ACES
11
Example
In a payroll program, what data
would we need to know about all
employees?
First name, last name, sin, employee
id, address, phone number, city,
province, postal code, etc.
Data used to calculate pay for
employees depends on the type of
employee!
7/18/2015
Wendi Jollymore, ACES
12
Example
Full time employees:
annual salary, weeks worked
Part time employees
hours worked, hourly rate
Sales employees
weekly sales, commission rate, base
pay.
Temporary sales employees
weekly sales, commission rate.
7/18/2015
Wendi Jollymore, ACES
13
Example
Writing each of the 4 classes with
all of the data and methods would
be tedious!
We usually take the common things
and put them in a parent class
Then the child classes contain all the
“extras”
And modify parent methods, if needed
7/18/2015
Wendi Jollymore, ACES
14
Example
Employee Class:
Employee
Data Members:
first name
last name
employee id
sin
phone number
address
city
province
postal code
Methods:
calculateWeeklyPay()
printCheck()
7/18/2015
Wendi Jollymore, ACES
15
Example
Child Classes:
FullTimeEmployee
extends Employee
PartTimeEmployee
extends Employee
Data Members:
annual salary
weeks worked
Data Members:
rate of pay
hours worked
Methods:
calculateWeeklyPay()
Methods:
calculateWeeklyPay()
7/18/2015
Wendi Jollymore, ACES
16
Example
Note that SalesEmployee and
TempSalesEmployee are very
similar:
SalesEmployee
TempSalesEmployee
extends Employee
extends Employee
Data Members:
commission rate
weekly sales
Data Members:
commission rate
weekly sales
base pay
Methods:
calculateWeeklyPay()
Methods:
calculateWeeklyPay()
7/18/2015
Wendi Jollymore, ACES
17
Example
We could make Sales Employee a
child class of Sales Employee
TempSalesEmployee
extends Employee
Data Members:
commission rate
weekly sales
Methods:
calculateWeeklyPay()
7/18/2015
SalesEmployee
extends
TempSalesEmployee
Data Members:
base pay
Methods:
calculateWeeklyPay()
Wendi Jollymore, ACES
18
Example
7/18/2015
Wendi Jollymore, ACES
19
One More Example
Vessels have a length characteristic and can
steer().
Sailboats and Destroyers are Vessels
both have length and can steer().
Sailboats have sails and can raiseJib(), but not
all vessels have these.
Destroyers have deckPlating and can fireGuns(),
but not all vessels have these.
What would a hierarchy chart look like with the
classes Vessel, Sailboat, and Destroyer?
7/18/2015
Wendi Jollymore, ACES
20
Possible Solution
Vessel
NavalVessel
MineSweeper
7/18/2015
PleasureCraft
Destroyer
PowerBoat
Wendi Jollymore, ACES
SailBoat
21
Exercise
For each of the following, which
classes would be parent classes,
which would be child classes?
Draw the hierarchy charts!
Loafer, Sneaker, Shoe, Sandal
Checking Account, Bank Account,
Savings Account
Cat, Cow, Feline, Animal, Chicken,
Bovine, Avian, Tiger, Yak, Parrot
7/18/2015
Wendi Jollymore, ACES
22
Robots and Inheritance
Look up the RobotSE class in the
documentation
(class discussion)
7/18/2015
Wendi Jollymore, ACES
23
Creating Your Own Robot
You can use Inheritance to create
your own Robot
You can give it your own methods like
faceDirection() and makeBox()
How about a collectThings() method
that gives the robot a certain number
of coloured things?
We’ll do this in class
7/18/2015
Wendi Jollymore, ACES
24