Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, School of Computing SOFT 120Page 1 11. Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7.

Similar presentations


Presentation on theme: "Mark Dixon, School of Computing SOFT 120Page 1 11. Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7."— Presentation transcript:

1 Mark Dixon, School of Computing SOFT 120Page 1 11. Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7 Williams and Walmsley (1999) sections 10.6, 10.7 and 10.8

2 Mark Dixon, School of Computing SOFT 120Page 2 Class Similarities common for applications to contain a number of classes that are closely related classes may have common attributes or operations classes may seem to represent different varieties of the same thing it would be useful to be able to model this type of relationship –often called a ‘is like a’ type of relationship –more formally: generalisation, and specialisation

3 Mark Dixon, School of Computing SOFT 120Page 3 Example: Animals Computer game could model different types of animal: Wolf Carnivore: Boolean Hungry: Boolean Eat() Drink() Howl() Lion Carnivore: Boolean Hungry: Boolean Eat() Drink() Roar() Cow Carnivore: Boolean Hungry: Boolean Eat() Drink() Moo() they have common attributes and operations

4 Mark Dixon, School of Computing SOFT 120Page 4 Inheritance: Animals it is better to separate common attributes and operations: Wolf Howl() Lion Roar() Cow Moo() Animal Carnivore: Boolean Hungry: Boolean Eat() Drink()

5 Mark Dixon, School of Computing SOFT 120Page 5 Example: Bank Accounts Generally banks allow customers different account types: Current Account No: integer Balance: double Overdraft: double Deposit() Withdraw() Cheque() Balance(): double Deposit Account No: integer Balance: double Deposit() Withdraw() AddInterest() Balance(): double High Interest Account No: integer Balance: double Deposit() Withdraw() AddInterest() Balance(): double they have common attributes and operations

6 Mark Dixon, School of Computing SOFT 120Page 6 Inheritance: Bank Accounts it is better to separate common attributes and operations: Current Overdraft: double Cheque() Deposit AddInterest() High Interest AddInterest() Account Account No: integer Balance: double Deposit() Withdraw() Balance(): double

7 Mark Dixon, School of Computing SOFT 120Page 7 Implementation in Delphi (1) Define the base class (super class) with all the common elements: type TAccount = class AccountNo: integer; Balance: double; procedure deposit(amount: double); procedure withdraw(amount: double); function GetBalance(): double; end;

8 Mark Dixon, School of Computing SOFT 120Page 8 Implementation in Delphi (2) Define the sub classes as derived from the base class: type TCurrent = class(TAccount) procedure Cheque(amount: double); end; the TCurrent class will inherit all of the attributes and operations from the TAccount class

9 Mark Dixon, School of Computing SOFT 120Page 9 Visibility visibility specifiers work with derived classes: –public: available to derived class, and outside –private: not available to derived class or outside –protected: available to derived class, but not outside type TAccount = class protected AccountNo: integer; Balance: double; public procedure deposit(amount: double); procedure withdraw(amount: double); function GetBalance(): double; end;

10 Mark Dixon, School of Computing SOFT 120Page 10 Polymorphism literally ‘having many shapes’ in OOP – different objects respond to the same message differently methods can be: –statically bound (default) –dynamically bound

11 Mark Dixon, School of Computing SOFT 120Page 11 Static Binding Variables of derived – static binding var MyPet: TWolf;... MyPet := TWolf.Create();... MyPet.Noise(); Here the compiler can include a direct call to the TWolf Noise method, as MyPet will always relate to an object of class TWolf

12 Mark Dixon, School of Computing SOFT 120Page 12 Dynamic Binding Variables of base class – dynamic binding var MyPet2: TAnimal;... MyPet2 := TWolf.Create();... MyPet2 := TAnimal.Create();... MyPet2.Noise(); the actual class of MyPet2 can change: being either the base class (TAnimal), or any derived class (e.g. TWolf, or TCow) Here the compiler cannot include a direct call to a specific Noise method, as MyPet2 may relate to objects of different classes at different points in time. Instead, the compiler inserts code that determines the type of MyPet2 at run time, and calls the appropriate method.

13 Mark Dixon, School of Computing SOFT 120Page 13 Virtual Methods allow same method to have different code for different objects descended from common ancestor each object that inherits a virtual method, can: –override the ancestor’s code (enhance or redefine capabilities) e.g. all animals may have talk method, but this will do different things depending on the specific animal

14 Mark Dixon, School of Computing SOFT 120Page 14 Example: Bank Accounts To declare a method as virtual in the base class: type TAccount = class … procedure withdraw(amount: double); virtual; … end; To override that method in the derived class: type TCurrent = class … procedure withdraw(amount: double); override; … end;

15 Mark Dixon, School of Computing SOFT 120Page 15 Abstract Methods and Classes Abstract methods: –a method in a base class with no associated code Abstract class: –base classes are sometimes abstract: that is there will never be an instance of them – only instances of their subclasses


Download ppt "Mark Dixon, School of Computing SOFT 120Page 1 11. Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7."

Similar presentations


Ads by Google