150 likes | 257 Views
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. Class Similarities. common for applications to contain a number of classes that are closely related
E N D
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
Class Similarities • common for applications to containa 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
Example: Animals • Computer game could model different types of animal: Wolf Lion Cow Carnivore: Boolean Hungry: Boolean Carnivore: Boolean Hungry: Boolean Carnivore: Boolean Hungry: Boolean Eat() Drink() Howl() Eat() Drink() Roar() Eat() Drink() Moo() • they have common attributes and operations
Inheritance: Animals • it is better to separate common attributes and operations: Animal Carnivore: Boolean Hungry: Boolean Eat() Drink() Wolf Lion Cow Howl() Roar() Moo()
Example: Bank Accounts • Generally banks allow customers different account types: Current Deposit High Interest Account No: integer Balance: double Overdraft: double Account No: integer Balance: double Account No: integer Balance: double Deposit() Withdraw() AddInterest() Balance(): double Deposit() Withdraw() AddInterest() Balance(): double Deposit() Withdraw() Cheque() Balance(): double • they have common attributes and operations
Inheritance: Bank Accounts • it is better to separate common attributes and operations: Account Account No: integer Balance: double Deposit() Withdraw() Balance(): double Current Deposit High Interest Overdraft: double AddInterest() AddInterest() Cheque()
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;
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
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;
Polymorphism • literally ‘having many shapes’ • in OOP – different objects respond to the same message differently • methods can be: • statically bound (default) • dynamically bound
Static Binding • Variables of derived – static binding var MyPet: TWolf; ... MyPet := TWolf.Create(); ... MyPet.Noise(); Here the compiler can include a direct call to theTWolf Noise method, as MyPet will alwaysrelate to an object of class TWolf
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.
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
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;
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