1 / 15

11. Object Associations (part 2): Inheritance and Polymorphism

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

adam-monroe
Télécharger la présentation

11. Object Associations (part 2): Inheritance and Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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

  3. 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

  4. Inheritance: Animals • it is better to separate common attributes and operations: Animal Carnivore: Boolean Hungry: Boolean Eat() Drink() Wolf Lion Cow Howl() Roar() Moo()

  5. 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

  6. 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()

  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. 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. 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. Polymorphism • literally ‘having many shapes’ • in OOP – different objects respond to the same message differently • methods can be: • statically bound (default) • dynamically bound

  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 theTWolf Noise method, as MyPet will alwaysrelate to an object of class TWolf

  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. 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. 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. 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

More Related