Classification and Inheritance - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Classification and Inheritance

Description:

we have defined classes (templates for objects) ... scooby doo. tony. george. bertie. Classification and Inheritance. CS2200. 4. Vehicle classification ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 21
Provided by: kenb97
Category:

less

Transcript and Presenter's Notes

Title: Classification and Inheritance


1
Classification and Inheritance
  • classification
  • inheriting properties and behaviours
  • example

2
Using objects
  • we have defined classes (templates for objects)
  • when we want an object of a particular class, we
    simply create one
  • we can re-use classes by using them in different
    programs
  • we can compose objects (by referring to other
    objects)
  • we can return objects from methods
  • ... but when we want a new class, we have to
    define it all from the beginning.
  • We will now look at a way of extending existing
    classes to get new, more specialised, class
    definitions.

3
Classification
  • Classification is a way of looking at the world
  • we organise things into classes
  • we cluster classes into bigger classes, and so on
    ...
  • members of a class have properties in common

Mammals
Apes
Dogs
Humans
Gorillas
Chimps
4
Vehicle classification
MotorVehicle
Car
MotorBike
Lorry
...
...
Fiat
Ford
BMW
...
Mondeo
Fiesta
Focus
Punto
Stilo
...
5
Classification in Computing Science
  • Classifying objects is one of the fundamental
    tasks for Computing Science and Artificial
    Intelligence
  • we want to write down our knowledge of the world
    so that computers can understand and manipulate
    it
  • how should we classify different objects?
  • are there different classifications?
  • how do I make sure my system is using the same
    classification as your system?
  • This will be studied in CS courses in later years.

6
Inheriting properties
  • In a classification hierarchy, if a class has a
    property, then all the classes that are beneath
    it also have that property.

Mammals
Apes
Dogs
Humans
Gorillas
Chimps
7
Inheritance in Java re-using classes
  • Objects in the Account class maintain a reference
    to the owner, a number, and a balance. They allow
    deposits, withdrawals, and balance checks. We
    used that class to represent both current
    accounts and savings accounts.
  • However, the two types of account should be
    slightly different
  • a current account has an associated laser card
  • a savings account has an interest rate and
    payments
  • The two types of account are both specialisations
    of the generic Account class. We should re-use
    its definition to create CurrentAccount and
    SavingsAccount classes.

8
The Account class
public class Account private double
balance //must be gt 0.0 private int
number //99999 lt number lt 1000000
private Customer holder //points up to the
Customer //(no argument) constructor
public Account() number -1
balance 0.0 holder null
//constructor - takes an id number and a
Customer public Account(int id, Customer c)
if (99999 lt id id lt 1000000)
number id else number -1 holder
c
9
//constructor - takes in a deposit, id number
and Customer public Account(int id, Customer
c, double sum) if (99999 lt id id lt
1000000) number id else number -1
holder c if (sum gt 0.0) balance
sum else balance 0.0 //get
methods public double getBalance() return
balance public int getNumber() return
number public String toString()
return "number " number "
balance " balance " "
10
//deposit a positive amount of money //if
successful, return true else return false
public boolean deposit(double sum) if
(sum gt 0.0) balance balance sum
return true else return
false //withdraw a sum of money //if
OK, change the balance and return the withdrawn
sum //else don't change the balance, and
return 0.00 public double withdraw(double sum)
if (balance gt sum) balance
balance - sum return sum
else return 0.00
11
Account
holder number balance
the subclasses inherit variables and methods from
the superclass
Account() toString() Account(...)
deposit(...) Account(...)
withdraw(...) getBalance()
12
Creating a subclass
  • // CurrentAccount.java
  • public class CurrentAccount extends Account
  • private int laserNumber
  • public int getLaserNumber()
  • return laserNumber
  • public double payByCard(double sum)
  • if (sum gt 0.0)
  • return withdraw(sum)
  • else
  • return 0.0

all Account methods and variables will be
inherited and are part of CurrentAccount
13
The SavingsAccount sub-class
// SavingsAccount.java public class
SavingsAccount extends Account private
double rate public double getRate()
return rate public double
addInterest() ...
14
Inheriting data and methods
  • All data in the superclass are inherited by the
    subclass
  • if we create an object from the subclass, then it
    will have all the data (variables and references)
    defined explicitly in the subclass, and also all
    data defined in the superclass
  • All methods in the superclass are inherited by
    the subclass
  • if we create an object from the subclass, then it
    will have all the methods defined explicitly in
    the subclass, and also all methods defined in the
    superclass

15
What objects in the sub-classes look like ...
acc1 CurrentAccount
laserNumber
holder number balance
getLaserNumber() payByCard(...)
Account() Account(...) Account(...)
getBalance() toString() deposit(...) withdraw(...)
16
import java.text.DecimalFormat public
static void main (String args)
CurrentAccount acc new CurrentAccount()
DecimalFormat doubleOut new DecimalFormat("0.00"
) System.out.println("Account created"
"\nBalance " doubleOut.format(acc.ge
tBalance()) "\nCard number "
acc.getLaserNumber()) System.out.println("D
epositing 100.0") if (acc.deposit(100.0)
! 100.0) System.out.println("Error
deposit should be 100.0") if
(acc.getBalance() ! 100.0)
System.out.println("Error balance should be
100.0") System.out.println("Paying 27.41
by card") if (acc.payByCard(27.41) !
27.41) System.out.println("Error
payment should be 27.41") if
(acc.getBalance() ! 72.59)
System.out.println("Error balance should be
72.59") System.out.println("Final balance
" doubleOut.format(acc
.getBalance()))
17
CurrentAccount acc new CurrentAccount()
... acc.deposit(100.0) ...
acc.payByCard(27.41) ...
18
Why use inheritance?
  • inheritance allows us to re-use designs and code
    written by us and by other people
  • alternative is to re-implement all the methods
    and variables you used in the other classes
  • this duplicates the code
  • more work for you
  • more difficult to maintain the code, ensuring an
    update is applied to every relevant class
  • confusing for people using your classes

19
Final Remarks
  • Some people believe you should avoid protected
  • subclasses should access inherited variables
    using public get and set methods in the interface
    of the superclass
  • this reduces coupling
  • What if you don't want to inherit all the
    instance variables?
  • Too bad! Instance variables are always inherited
  • Try reworking your hierarchy

20
Next lecture ...
  • the protected visibility modifier
  • overriding methods
  • inheritance and constructors
  • the super method call
Write a Comment
User Comments (0)
About PowerShow.com