Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Inheritance and Polymorphism"— Presentation transcript:

1 Inheritance and Polymorphism
An Introduction Copyright © Curt Hill

2 Biology Example Class is a borrowed word from biology
One line from an inheritance tree Animals (a kingdom) Vertebrates (a phylum) Mammals (a class) Rodents (an order) Squirrels (a family) Thirteen line ground squirrel (a specie) Copyright © Curt Hill

3 Relationship Characteristics are preserved as we go down the line of inheritance The prior list shows an is a relationship A thirteen line ground squirrel is a squirrel A squirrel is a rodent A thirteen line ground has all the characteristics of any of the above Copyright © Curt Hill

4 Biology Example Again Mammals are warm blooded, have fur and feed young milk Rodents have 1 pair of upper incisor teeth and are mammals Squirrels have furry tails and are rodents Copyright © Curt Hill

5 A specific instance A thirteen line ground squirrel is a squirrel
Does a thirteen line ground squirrel have warm blood? 1 pair of upper incisors? furry tail? Yes: a thirteen line ground squirrel is a squirrel, rodent, mammal, vertebrate and animal Copyright © Curt Hill

6 Inheritance Deriving new classes from old classes
New class may retain any properties or methods of old New class may add new properties, new methods New class may replace/remove old methods Remember the platypus, which is an egg laying mammal Copyright © Curt Hill

7 Copying and Deriving What is the difference?
After the copy, the two classes are separate and no longer related After a derivation the derived class can be changed by changing the original Copyright © Curt Hill

8 Inheritance example Person has a name and age
Student is a person with GPA and major Employee is a person with a wage Grad is a student with degree The characteristics of a person are still contained in a grad Copyright © Curt Hill

9 person set_name employee student set_wage set_major grad set_degree
Copyright © Curt Hill

10 Inherited properties and methods
All classes have properties (eg. name and age) and methods (eg. set_name) Person explicitly declares it All the rest inherit it Grad has degree, as well as GPA and major Copyright © Curt Hill

11 Terminology of inheritance
Person is the base class or superclass Student and Employee are derived classes Copyright © Curt Hill

12 Displaying the contents
Each class needs a display method How would that work? Each class would call the display of its ancestor Then display the things that are unique to it Much easier that re-doing the work of the older classes If the ancestral class changes the display routine the descendants would take benefit without any change Copyright © Curt Hill

13 Polymorphism Literally: many shapes
Classes that are related are interchangable In C++ only the pointers are interchangable In Java any container, such as an array can have related contents Copyright © Curt Hill

14 Effects of Polymorphism
Do a display on any class derived from person Not know until run-time, which kind it is Determining the function to use is called binding Dynamic binding vs static binding Copyright © Curt Hill

15 Binding Most programming languages use static (or early) binding
At compile or link time determine the function needed Examples: Pascal, C, COBOL Some programming languages use dynamic (or late) binding Determine function needed at run time Examples: LISP and Java C++ uses both, depending on situation Copyright © Curt Hill

16 Inherited functions Execute the set_name function against any variable of these types derived from person The same effect occurs Defined in the base type, person Variable is upcast, that is converted to base type This is not polymorphism Copyright © Curt Hill

17 Polymorphism Execute the display function against any variable of these four types The different effects occur based on the actual type of the variable Calls display knowing that all person derived classes have a display function They inherit it or redefine it A function can accept a person type and receive either a person, student, employee or grad without problem Copyright © Curt Hill

18 Virtual Functions If two classes in the same inheritance tree have the same function they may be virtual Such as display in the person hierarchy Calling a virtual function with any class in the tree and you get that classes function, without knowing until run-time which class you have This is the basis of polymorphism Copyright © Curt Hill

19 Requirements of Polymorphism
The class must be accessed via pointer Only way in Java The pointer must be of a base class of any possible polymorphic methods The method in question must be virtual It must have same signature Copyright © Curt Hill

20 Rodents in North America
Rodents are an order consisting of several families Squirrels Pocket gophers Pocket Mice and Kangeroo Rats Beaver Mice, Rats, Voles, Lemmings Old world Rats and Mice Jumping Mice Porcupines Copyright © Curt Hill

21 Natural inheritance trees
In nature the inheritance tree consists only of leaves No examples of rodents that do not belong to one of the rodent families No generic rodent that is not a squirrel, not a ... Generic rodent is group of characteristics not an animal Copyright © Curt Hill

22 Object inheritance trees
Programming language object hierarchies may have objects that are branches and leaves We may instantiate a person or a student An ancestral object may be more than a group of characteristics Objects may also be groups of characteristics Copyright © Curt Hill

23 Abstract Base Type A type that is not instantiatable
Only used to define the interface (the characteristics) for derived classes Consider a generic rodent Has the characteristics of rodent but not a member of any of the subordinate families Copyright © Curt Hill

24 Abstract Base Class Example
Consider a Shape class Want to be able to: Move a shape Draw a shape The Shape class then has descendents: Circle Square The Shape class may implement move but cannot implement draw The Shape class forces all of its descendents to implement draw Copyright © Curt Hill

25 Pure Virtual Functions
Only interface if specified Such as draw Every abstract base class has one or more pure virtual functions No class with pure virtual functions may be instantiated An instantiable function overrides the pure virtual with a callable function Copyright © Curt Hill

26 Conclusion Two ideas: Reuse as much code as possible
Maximize the flexibility Copyright © Curt Hill


Download ppt "Inheritance and Polymorphism"

Similar presentations


Ads by Google