Inheritance and Polymorphism

Inheritance:

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

Conceptually, a derived class is a specialization of the base class. For example, if you have a base class Animal, you might have one derived class that is named Mammal and another derived class that is named Reptile. A Mammal is an Animal, and a Reptile is an Animal, but each derived class represents different specializations of the base class.

  • When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and destructors.
  • The derived class can thereby reuse the code in the base class without having to re-implement it.
  • In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class.

C# supports two types of Inheritance mechanisms:

1) Implementation Inheritance
2) Interface Inheritance

Implementation Inheritance:

– When a class (type) is derived from another class(type) such that it inherits all the members of the base type it is Implementation Inheritance

Interface Inheritance:

– When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance

In general Classes can be derived from another class, hence support Implementation inheritance At the same time Classes can also be derived from one or more interfaces Hence they support Interface inheritance Structs can derive from one more interface, hence support Interface Inheritance Structs cannot be derivedfrom another class they are always derived from SystemValueType

Multiple Inheritance:

C# does not support multiple implementation inheritance A class cannot be derived from more than one class However, a class can be derived from Multiple Interfaces.

————————————

Inheritance Syntax:

Class derivedClass:baseClass
{
}

Interface Inheritance:

private Class derivedClass:baseClass , InterfaceX , InterfaceY
{
}

Abstract And Virtual Method:

When a base class declares a method as virtual, a derived class can override the method with its own implementation.

class baseClass
{
public virtual int fnCount()
{
return 10;
}
}

class derivedClass :baseClass
{
public override int fnCount()
{
return 100;
}
}

If a base class declares a member as abstract, that method must be overridden in any non-abstract class that directly inherits from that class.

If a derived class is itself abstract, then it inherit abstract members without implementing them.

Abstract and virtual members are the basis for polymorphism, which is the second primary characteristic of object-oriented programming.

Polymorphism:

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance.

  • At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object’s declared type is no longer identical to its run-time type.
  • Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class’s version of the method to be executed.

Virtual methods enable you to work with groups of related objects in a uniform way.

For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. You do not know at compile time which specific types of shapes the user will create. However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions. You can use polymorphism to solve this problem in two basic steps:

  • Create a class hierarchy in which each specific shape class derives from a common base class.
  • Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.

Example:

public class Shape
{
// A few example members
public int X { get; private set; }
public int Y { get; private set; }
public int Height { get; set; }
public int Width { get; set; }

// Virtual method
public virtual void Draw()
{
Console.WriteLine("Performing base class drawing tasks");
}
}

class Circle : Shape
{
public override void Draw()
{
// Code to draw a circle...
Console.WriteLine("Drawing a circle");
base.Draw();
}
}
class Rectangle : Shape
{
public override void Draw()
{
// Code to draw a rectangle...
Console.WriteLine("Drawing a rectangle");
base.Draw();
}
}
class Triangle : Shape
{
public override void Draw()
{
// Code to draw a triangle...
Console.WriteLine("Drawing a triangle");
base.Draw();
}
}

class Program
{
static void Main(string[] args)
{
// Polymorphism at work #1: a Rectangle, Triangle and Circle
// can all be used whereever a Shape is expected. No cast is
// required because an implicit conversion exists from a derived
// class to its base class.
System.Collections.Generic.List shapes = new System.Collections.Generic.List();
shapes.Add(new Rectangle());
shapes.Add(new Triangle());
shapes.Add(new Circle());

// Polymorphism at work #2: the virtual method Draw is
// invoked on each of the derived classes, not the base class.
foreach (Shape s in shapes)
{
s.Draw();
}

// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}

}
-----------------------------------------

/* Output:
Drawing a rectangle
Performing base class drawing tasks
Drawing a triangle
Performing base class drawing tasks
Drawing a circle
Performing base class drawing tasks
*/

------------------------------------------

Source: MSDN & Exforsys

About ranjith
Software Engineer...

Leave a comment