[C# Tutorial 8] Polymorphism and Inheritance

2

Inheritance

To learn about concepts of inheritance and polymorphism, the basic thing required is knowledge of classes and methods.

In English dictionary, the meaning of inheritance can be stated as “getting something from upper hierarchy”. In C-Sharp, apply same concept using classes. The following diagram will clear the concept of inheritance in C-Sharp

 

The above block diagram, shows that 2 classes called as 2-Wheeler and 4-Wheeler are derived from parent class here known as “vehicle”. This process of deriving a class from other class is called as inheritance. Some of the features of inheritance are as follows:

 

  1. The parent class is also known as base class/super class and child class is also known as derived class/ sub class.
  2. Using inheritance saves memory and time required to write the same code in derived class.
  3. With addition to the members of child class, every public member of parent class are also inherited and become the members of child class.                                              Example: If vehicle class has a variable no_plate and a method show_no(). and class 2-wheeler and 4-wheeler will have these 2 members inherited from class vehicle and also will contain the members that are defined in respective class                                       NOTE: Member resembles data variables and methods
  4. Inheritance also gives rise to new concept called polymorphism.
  5. In CSharp a class can inherit other class using colon(:).                                                   Syntax: class derived_class_name : Base_class_name                             
  6. A class can inherit only a single class and can implement multiple interfaces. Note: The concept of interfaces will be explained in further tutorials.

Example :

namespace ConsoleApplication1
{
class Program :parent       //Program class inherits parent class
{
static void Main(string[] args)
{
Program p = new Program();
p.add();    //calling parent class method with child class object
Console.Read();
}
}
class parent                        //Base/Parent/Super Class
{
public int p_a,p_b,p_result;        //public variables
public parent()                    // Constructor
{
p_a = 10;
p_b = 20;
}
public void add()                   //public method
{
p_result = p_b + p_a;
Console.WriteLine(“result : ” + p_result);
}
}
}

Output:

In the above example a class with name parent is created containing 3 variables, 1 constructor and 1 method. This class is then inherited by class Program. In the main method a object is created on class Program, and with that object the method of the base class is called .

 

Polymorphism:

Poly means more than 1, and morph means faces, Therefore combining together it means more than 1 faces. With respect to C#, this concept is applicable to methods, where two or more methods are declared with same name. Now , a question arises  if more than 1 method are of same name then if the method is called, then which is to be executed ??? The solution to this is the parameters of method.

The example of polymorphism are:

Overloading:

A method is said to be overloaded when 2 or more methods are of same name but different parameters (signatures). Parameters are those values that are passed to the method when they are called.

The number and type of values that are to be passed are specified in the method declaration.Firstly, the number of parameters of the two methods are checked, if found same then the data type of parameter is checked.

 

    class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.add(5,5);
p.add(5.235232,3.235356);
Console.Read();
}

void add(int x, int y)      //1st add method with 2 int parameters
{
int result = x + y;
Console.WriteLine(“Integer Result : “+ result);
}

void add(double x, double y)    //2nd add method with 2 double parameters
{
double result = x + y;
Console.WriteLine(” double Result : “ + result);
}

}

 

Output:

In the above code a class Program is declared which contains two ‘add’ method with different signatures. Both method has 2 parameters one with int data type and other with double data type. So we can say that method add is overloaded…

Overriding:

This is another variant of polymorphism, in which 2 or more methods have same name and same parameters. A method can be overloaded if it belongs to different classes in the same hierarchy of inheritance. Two keywords are essentially required are “virtual” and “override“. Virtual keyword is used to make virtual method in base class. Only virtual method can be overridden. Or in other words any method which is to be overloaded needs to be declared as virtual in base class. Override keyword is used in derived class preceding to the method declaration which is to be override. The overridden method will have priority on original method. To execute the original method base keyword is used as shown in following example.

 

namespace ConsoleApplication1
{
class Base                     //Base class
{
public virtual void method1()
{
Console.WriteLine(” Base Class — Method1″);
}
}

    class Program : Base            //Derived Class
{
static void Main(string[] args)
{
Program obj = new Program();
obj.method1();

Console.Read();
}
override public void method1()  //Overrided method
{
base.method1();     //calling base class method
Console.WriteLine(” CLass Program — Method1″);
}
}
}

 

Output :


The method “method1” is overridden in Program  class, which is inherited by Base class,

Share.

About Author

2 Comments

    • @Rahul, There are some articles remaining on CSharp language which will help you in understanding this language in more detail and if more information you require about particular topic then let me know, and speaking about interview Questions, The imp Q’s that may be asked can be about OOP(Object Oriented Programming) that covers all its features and newly added features which will look in coming articles. Apart from this interviewer is also interested in .net topics like ASP.NET, WCF, ADO.NET, WPF etc…

Leave A Reply