Inheritance – Polymorphism


Inheritance-Polymorphism

 

In this article I will explain polymorphism. What are different types of polymorphism? The use of method overloading, virtual method, method hiding, method shadowing and method overriding.

 

Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming. Inheritance is transitive in nature.

 

Types of polymorphism

 

There are two types of polymorphism:

 

1.   Compile time polymorphism

2.   Run time polymorphism.

 

Compile Time Polymorphism

 

Compile time polymorphism is method and operators overloading. It is also called early binding.

 

In method overloading method performs the different task at the different input parameters.

 

Runtime Time Polymorphism

 

Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

 

When overriding a method, you change the behavior of the method for the derived class.  Overloading a method simply involves having another method with the same prototype.

 

Note:  C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.

 

Practical example of Method Overloading (Compile Time Polymorphism)

 

using System;
namespace
method_overloading
{
class Program
{
public class Print
{

public void display(string name)
{
Console.WriteLine("Your name is : " + name);
}
public void display(int age, float marks)
{
Console.WriteLine("Your age is : " + age);
Console.WriteLine("Your marks are :" + marks);
}

}

static void Main(string[] args)
{
Print obj = new Print();
obj.display(
"George");
obj.display(34, 76.50f);
Console.ReadLine();

}
}
}
 

In the code if you observe display method is called two times. Display method will work according to the number of parameters and type of parameters.

 

Inheritance can be seen in following context:

 

Virtual Method

Method Hiding

Method Shadowing

Method Overloading

 

Virtual Method

 

Virtual means the method can be over-ridden in classes that derive from the base-class with the virtual method in.

 

You could derive from a class with a virtual method, and re-define the virtual method with new instead of the override keyword.

 

Method Hiding

 

In this process derived class method will hide the method of base. Method hiding is implicit process. (It can be by mistake and will give you warning)

 

Practical example of Method Hiding


using System;
namespace
Method_hiding
{
class Program
{
public class BaseClass
{
string name;
public BaseClass(string name)
{
this.name = name;
}
public void display()
{
Console.WriteLine("Base class name is : " + this.name);
}
}
public class DerivedClass : BaseClass
{
string dname;
public DerivedClass(string dname) : base ("First")
{
this.dname = dname;
}
public void display()
{
Console.WriteLine("Derived Class name is : " + this.dname);
}
}
public static void Main(string[] args)
{
BaseClass ob1 = new BaseClass("First");
DerivedClass ob2 = new DerivedClass("Second");
ob1.display();
ob2.display();
 
Console.ReadLine();
}
}
}
 

Method Shadowing

 

Method shadowing is an explicit process. Shadowing has existence of both the methods that is of base class as well as derived class.

 

You can't shadow more than one time (one time inheritance)

 

Practical example of Method Shadowing

 

using System;
namespace
Method_shadowing
{
class Program

{
public class BaseClass
{
string name;
public BaseClass(string
name)
{
this
.name = name;
}
public void
display()
{
Console.WriteLine("Base class name is : "
+ name);
}
}
public class DerivedClass : BaseClass

{
string derivedName;
public DerivedClass(string derivedName) : base ("First"
)
{
this
.derivedName = derivedName;
}
public new void
display()
{
Console.WriteLine("Derviced class new name is : "
+ derivedName);
}
}

static void Main(string
[] args)
{
BaseClass ob1 = new BaseClass("First"
);
ob1.display();
DerivedClass ob2 = new DerivedClass("Second"
);
ob2.display();
Console
.ReadLine();
}
}
}

You mark derived class method with new keyword in method shadowing.

 

In method shadowing you have both the methods available. You are only changing the functionality prototype remains same. .

 

You can call the new method and the method defined in the base class. Existence of both base class and derived class method in managed heap.

 

Method Overriding

 

Overriding is a way to optimize code. Overriding process overwrites the method of base class and only one method exists in the managed heap.

 

You can override to N level. (To stop overriding we use sealed method) In overriding we can't change the method prototype but can change its functionality.

 

Overriding is runtime polymorphism.

 

Sealed method is used to define overriding level of a virtual method. Sealed keyword is always used with override key word.

 

Practical example of Method Overriding

 

using System;
namespace
method_overriding
{
class Program

{
public class BaseClass
{
string name;
public BaseClass(string
name)
{
this
.name = name;
}
public virtual void
display()
{
Console.WriteLine("Base class method " + this
.name);
}
}
public class DerivedClass : BaseClass

{
string derivedName;
public DerivedClass(string
derivedName)
:
base("First"
)
{
this
.derivedName = derivedName;
}
public override void
display()
{
Console.WriteLine("Derviced class method name : "
+ derivedName);
}
}
static void Main(string
[] args)
{
BaseClass ob1 = new BaseClass("First"
);
ob1.display();
DerivedClass ob2 = new DerivedClass("Second"
);
ob2.display();
Console
.ReadLine();
}
}
}
 

Note: virtual method only changes functionality but new method creates a new method.

 

I think now you would be having a good knowledge of inheritance (compile time and runtime polymorphism, Virtual, hiding, shadowing and overriding methods.)