Inheritance and Polymorphism An Introduction Copyright © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
person set_name employee student set_wage set_major grad set_degree Copyright © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
Terminology of inheritance Person is the base class or superclass Student and Employee are derived classes Copyright © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
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 © 2000-2012 Curt Hill
Conclusion Two ideas: Reuse as much code as possible Maximize the flexibility Copyright © 2000-2012 Curt Hill