You are on page 1of 27

Inheritance

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:

• In C#, a virtual function is always considered to be the root of virtual


dispatch; that is, once C# finds a virtual method, it looks no further up
the inheritance hierarchy (so no problem with above method)
Versioning with new and override keywords
• The compiler generates the following warning, When ListBox is
compiled again

• So Change your definition as follows, done


Abstract Classes
• An abstract method has no implementation. It creates a method
name and signature that must be implemented in all derived classes.
• Furthermore, making one or more methods of any class abstract has
the side effect of making the class abstract
• it is not legal to instantiate an object of an abstract class. Once you
declare a method to be abstract, you prohibit the creation of any
instances of that class.
• If the derived class of an abstract class failed to implement the
abstract method, that class would also be abstract, and again no
instances of that derived class would be possible
abstract public void DrawWindow( );
Abstract Classes
• If one or more methods are abstract, the class definition must also be
marked abstract, as in the following:
abstract public class Window
Abstract Classes
Abstract Class - Purpose
• It serves to implement the abstraction "Window" that will be manifest
in the various concrete instances of Window, such as browser
window, frame, button, list box, drop-down, and so forth.
• The abstract class establishes what a Window is, even though we
never intend to create a "Window" per se
Sealed Class
• a sealed class does not allow classes to derive from it at all.
• Classes are most often marked sealed to prevent accidental
inheritance.
• Placed before the class declaration, the sealed keyword precludes
derivation
Object - The Root of all Classes
• All C# classes, of any type, are ultimately derived from System.Object
• A base class is the immediate "parent" of a derived class. A derived
class can be the base to further derived classes, creating an
inheritance "tree" or hierarchy.
• A root class is the topmost class in an inheritance hierarchy. In C#, the
root class is Object.
Boxing and Unboxing Types
• Boxing - enables value types like integers to be treated as reference
types (objects). The value is "boxed" inside an Object ( and
subsequently "unboxed“ back to a value type)
• Boxing is an implicit conversion of a value type to the type Object.
• Console.WriteLine( ) expects an object, not an integer. To
accommodate the method, the integer type is automatically boxed by
the CLR, and ToString( ) is called on the resulting object.
Unboxing – Must be explicit
1. Make sure the object instance is a boxed value of the given value
type.
2. Copy the value from the instance to the value-type variable.
Nesting Classes
• At times, the contained class might exist only to serve the outer class,
and there might be no reason for it to be otherwise visible. (In short,
the contained class acts as a helper class.)
• You can define the helper class within the definition of the outer
class.
• The contained, inner class is called a nested class, and the class that
contains it is called, simply, the outer class.
• A method of a nested class can access private members of the outer
class.
• the nested class can be hidden from all other classes -- that is, it can
be private to the outer class.

You might also like