how to implement polymorphism in C-#(SHARP) application
Polymorphism is one of the most impotent feature of OOP that is used to exhibit different form of any particular procedure. With the help of polymorphism , you can use one procedure as many whys as per your requirement.
let's consider it with an example :
you can make a procedure for calculating the area of geometrical figure and can calculate the are of circle , triangle , or rectangle with same procedure and different parameters for each geometrical figure.
the advantages of polymorphism are as follow:
let's consider it with an example :
you can make a procedure for calculating the area of geometrical figure and can calculate the are of circle , triangle , or rectangle with same procedure and different parameters for each geometrical figure.
the advantages of polymorphism are as follow:
- Allow you to invoke methods of a derived class through base class reference during runtime.
- Provide different implementation of method in a class that are called through the same name.
basic syntax :
class baseclass
{
public void basemethod()
{
// method body
}
}
class derivedclass : baseclass
{
public void derivedmethod()
{
// method body
}
}
There are two type of polymorphism , which are as follow:
- Static polymorphism / compile time polymorphism / overloading
- Dynamic polymorphism / run time polymorphism / overriding
Compile time polymorphism:
there are the two type of compile time polymorphism
- method overloading:
- operator overloading:
method overloading: Method overloading is a concept in which a method behaves according to the number and type of parameters passed to it. In method overloading , you can define many method with the same name but different sing natures. A methods signature is the combination of the method name's , along with the number , type of order of the parameters.
.shoeing the code of method overloading application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace method_overloading.cs
{
public class shape
{
public void area(int side)
{
int squarera = side * side;
Console.WriteLine("area of squar is:" + squarera);
}
public void area(int lenth, int breadth)
{
int rectanglearea = lenth * breadth;
Console.WriteLine("area of rectangle is:" + rectanglearea);
}
public double area(double Base, double height)
{
double trianglearea = (Base * height) / 2;
Console.WriteLine("the area of triange is:" + trianglearea);
return trianglearea;
}
public void area(double radius)
{
double circlearea = 3.14 * radius * radius;
Console.WriteLine("the area of circle is:" + circlearea);
}
}
class Program
{
static void Main(string[] args)
{
shape Shape = new shape();
Shape.area(10);
Shape.area(10,10);
Shape.area(10.10,10.10);
Shape.area(10.00);
Console.ReadKey();
}
}
}
operator overloading: We know that , all the operator have specified meaning and functionality , such as the + (plus) operator add two numbers and - ( minus) operator subtract two number. However , you can change the functionality of an operator by using overloading them.When overload an operator , you need to create a method that must be preceded by operator keyword.
showing the code for operator overloading application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace operator_overloading.cs
{
class uninaryoperator
{
private int Number1, Number2;
public uninaryoperator()
{
}
public uninaryoperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public void showdata()
{
Console.WriteLine("the number are :" + Number1 + "and" + Number2);
}
public static uninaryoperator operator -(uninaryoperator opr)
{
uninaryoperator obj = new uninaryoperator();
obj.Number1 = -opr.Number1;
obj.Number2 = -opr.Number2;
return obj;
}
}
class Program
{
static void Main(string[] args)
{
uninaryoperator opr1 = new uninaryoperator(20,30);
Console.WriteLine("before operatore ovrloading");
opr1.showdata();
uninaryoperator opr2 = new uninaryoperator();
opr2 = -opr1;
Console.WriteLine("==============================================");
Console.WriteLine("after operator overloading");
opr2.showdata();
Console.ReadKey();
}
}
}
Runtime polymorphism / overriding: override is a feature that allow a derived class to provide a specific implementation of a method that is already define in a base class. The implementation of a derived class override or replace the implementation of method in a base class. This feature is also known as "runtime polymorphism".
To invoke the method of a derived class that is already define in a base class , you need to perform the following steps.............
- declare the base class method as "virtual"
- Implement the derived class method using the "override" keyword.
basic syntax:
class baseclass
{
public virtual void showdata()
{
// method body
}
}
class derivedclass : baseclass
public override void showdata()
{
// method body
}
showing the code of runtime polymorphism:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace overloading.cs
{
class person
{
private int fage;
public person()
{
fage = 21;
}
public virtual void setage(int age)
{
fage = age;
}
public virtual int getage()
{
return fage;
}
}
class adultperson : person
{
public adultperson()
{
}
override public void setage(int age)
{
if (age > 21)
base.setage(age);
}
}
class Program
{
static void Main(string[] args)
{
person p = new person();
p.setage(18);
adultperson ad = new adultperson();
ad.setage(18);
Console.WriteLine("person age is :"+ p.getage());
Console.WriteLine("person age is :" + ad.getage());
Console.ReadKey();
}
}
}
0 comments: