Reader Level:
ARTICLE

The third pillar of object-oriented programming - polymorphism:

Posted by Amr Monjid Articles | Visual C# May 29, 2008
In the last part of this series you will see the third pillar of object-oriented programming (polymorphism); you will see how the polymorphism gives you the ability to treat related objects in the same way.
  • 0
  • 0
  • 14762

Introduction:

 

You can define polymorphism as the ability of treating related types in the same way.  Polymorphism give the base class the ability of defining a set of behaviors that the childe classes can override, so each childe class will redefine how it will respond to the same behavior.

 

The virtual methods:

 

A virtual is a method in a base class which the childe class can reuse it or redefine and customize its behavior to be appropriate to its functionality.

 

To create an overridable method that can be overridden by childe class you must define this method as (virtual)

 

//this method can be overridden by any childe class 

public virtual void Move() 

{ 

    //do some thing in the base class 

} 

Example:

 

public class Car

{

    string model;

    int currentSpeed;

    int maxSpeed;

 

    public Car()

    { }

 

    //this method can be overridden by any childe class

    public virtual void Move()

    {

        //do some thing in the base class

        Console.WriteLine("Your car is moving now");

    }

}

If any childe class want to redefine the virtual method it need to use the override keyword, so if we want to override the Move() virtual method in the base class Car we need to do the following:

 

public class SportCar : Car

{

    public override void Move()

    {

        //Write a new implementation here

    }

}

Using the default implementation of the virtual method:

 

When you override a virtual method from the base class, you can use the default implementation of the base class method. You can do this by using the (base) keyword to call the virtual method which in the base class.

 

As you can see in the overridden method, we used the base keyword to call the Move method in the base class so we are using now the implementation of the Move() method in the base class.

 

 

public override void Move()

{

    //The default implementation

    base.Move();

}

Example:

 

SportCar sportsCar1 = new SportCar();

       

//Calling the overridden method

sportCar1.Move();

The result:

 

Your car is moving now

 

If you do not want to use the default implementation of  the base class method you can remove the (base.Move) form the overridden method and then reimplement the method by writing a new code that belong to it as follow:

 

public override void Move()

{

    //Write a new implementation here

    Console.WriteLine("Sport car is moving now");

}

Example:

 

SportCar sportsCar1 = new SportCar();

//Calling the overridden method

sportCar1.Move();

The result:

 

Sport car is moving now

 

You can also combine by using the virtual   method implementation and the overridden method implementation by keeping the (base.Move) in the overridden method implementation  

 

public override void Move()

{

    //Using the default implementation

    base.Move();

 

    //Write a new implementation here

    Console.WriteLine("Sport car is moving now");

}

Note that the overridden method considered as virtual method to the subclasses of the subclass. So if create a new class that inherit from the SportCar class, you can override the Move() method in at as if it is a virtual method.

 

Example:

 

public class SportCar : Car

{

    public override void Move()

    {

        //Write a new implementation here

        Console.WriteLine("Sport car is moving now");

    }

}

 

public class RedSportCar : SportCar

{

    public override void Move()

    {

        //Using the SportCar.Move() method

        base.Move();

    }

}

Note: SportCar.Move() method work as a virtual method to the RedSportCar class.

 

The sealed keyword:

 

You can use the sealed keyword with virtual methods if you want to prevent the subclasses form overridden the base classes methods.

 

Example:

 

public class SportCar : Car

{

    //This method can not be overridden by subclasses

    public override sealed void Move()

    {

        //Write a new implementation here

        Console.WriteLine("Sport car is moving now");

    }

}

 

public class RedSportCar : SportCar

{

    //Compile time error

    //The SportCar.Move() can not be overridden

    public override void Move()

    {

        base.Move();

    }

}

The abstract methods:

 

If you create a virtual method in the base class it is up to the subclass to override or not to override it. What if you want to enforce the subclasses to extend the base class members? To do so you need to use abstract methods.

 

Abstract methods are a method with no implementation, and it is a part of an abstract class. Any subclass that derived from this base abstract class must override the abstract method or there will be a compile time error.

 

Example:

 

public abstract class Car

{

    //the abstract method

    //abstract method doesn't have a body

    public abstract void Move();

}

 

public class SportCar : Car

{

    //the SportCar class must override the

    //abstract method Move().

    public override void Move()

    {

        //Write a new implementation here

        Console.WriteLine("Sport car is moving now");

    }

}

Note: Only abstract classes can have abstract methods.

 

Thank you for reading, see you next article.

COMMENT USING

Trending up