Inheritance and Polymorphism
Inheritance and Polymorphism
and
Polymorphism
Prof. M Nataraja Suresh
Specialization and Generalization
• The is-a relationship is one of specialization.
• When we say that a Dog is-a mammal, we mean that the dog is a
specialized kind of mammal.
• It has all the characteristics of any mammal (it bears live young,
nurses with milk, has hair), but it specializes these characteristics to
the familiar characteristics of canine domesticus.
• The specialization and generalization relationships are both reciprocal
and hierarchical
Ex: Dog and Cat specialize Mammal, and Mammal generalizes from
Dog and Cat
Specialization and Generalization Cont..
• These relationships are hierarchical because they create a relationship
tree, with specialized types branching off from more generalized
types.
• As you move up the hierarchy you achieve greater generalization
Ex: You move up toward Mammal to generalize that Dogs and Cats
and Horses all bear live young. As you move down the hierarchy you
specialize
• When two classes share functionality, and then to factor out these
commonalities into a shared base class. This provides you with
greater reuse of common code and easier-to-maintain code
Inheritance
• specialization relationship is typically implemented using inheritance
• Saying that ListBox inherits from (or derives from) Window indicates
that it specializes Window.
• Window is referred to as the base class, and ListBox is referred to as the
derived class.
• That is, ListBox derives its characteristics and behaviors from Window
and then specializes to its own particular needs.
public class ListBox : Window
• The derived class inherits all the members of the base class, both
member variables and methods.
• The derived class is free to implement its own version of a base class
method. It does so by marking the new method with the keyword new.
Polymorphism
• There are two powerful aspects to inheritance. (1) code reuse (2)
polymorphism.
• Poly means many and morph means form. Thus, polymorphism refers
to being able to use many forms of a type without regard to the
details.
Creating Polymorphic Methods
• To create a method that supports polymorphism, you need only mark
it as virtual in its base class.
• For example, to indicate that the method DrawWindow( ) of class
Window as polymorphic, simply add the keyword virtual to its
declaration, as follows:
public virtual void DrawWindow( )
Polymorphism
• Now each derived class is free to implement its own version of
DrawWindow( ).
• To do so, simply override the base class virtual method by using the
keyword override in the derived class method definition, and then
add the new code for that overridden method.
Polymorphism cont...
• So far, nothing polymorphic has been done. The real magic starts
when you create an array of Window objects and loop through as
below
Polymorphism cont...
• If you had not marked DrawWindow as virtual, Window's
DrawWindow( ) method would be called three times.
• However, because you did mark DrawWindow( ) as virtual and
because the derived classes override that method, when you call
DrawWindow( ) on the array, the compiler determines the runtime
type of the actual objects (a Window, a ListBox and a Button) and calls
the right method on each. This is the essence of polymorphism.
Versioning with new and override keywords
• assume for a moment that the Window base class of the previous
example was written by Company A.
• Suppose also that the ListBox and RadioButton classes were written
by programmers from Company B using a purchased copy of the
Company A Window class as a base.
• The programmers in Company B have little or no control over the
design of the Window class, including future changes that Company A
might choose to make
• Now suppose that one of the programmers for Company B decides to
add a Sort( ) method to ListBox:
Versioning with new and override keywords
• This presents no problems until Company A, the author of Window,
releases Version 2 of its Window class, and it turns out that the
programmers in Company A have also added a Sort( ) method to their
public class Window: