What is polymorphism?
2 vote(s)
Rating: 5 out of 5
Posted on: 2/8/2009 4:06:14 PM by Syedshakeer | Views: 6803 | Category: OOPS | Level: Beginner | Print Article
Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.
There Are Two Types of Polymorphism
1. Static Polymorphism
2. Dynamic Polymorphism
1. Static Polymorphism
In Static Polymorphism ,Which method is to be called is decided at compile-time only. Method Overloading is an example of Static Polymorphism.
Method overloading is a concept where we use the same method name many times in the same class,but different parameters. Depending on the parameters we pass, it is decided at compile-time only, which method is to calles. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permits. In Static Polymorphism decision is taken at compile time.
public Class StaticPolyDemo
{
public void display(int x)
{
Console.WriteLine(“Area of a Square:”+x*x);
}
public void display(int x, int y)
{
Console.WriteLine(“Area of a Square:”+x*y);
}
public static void main(String args[])
{
StaticPolyDemo spd=new StaticPolyDemo();
Spd.display(5);
Spd.display(10,3);
}
}
2. Dyanamic Polymorphism
In this Mechanism by which a call to an overridden function is resolved at a Run-Time( not at Compile-time). If a BaseClass contains method must be a Virtual method in C# to be overridden..
Class Test
{
Public virtual void show()
{
Console.WriteLine(“From base class show method”);
}
}
Public Class DynamicPolyDemo : Test
{
Public override void show()
{
Console.WriteLine(“From Derived Class show method”);
}
Public static void main(String args[])
{
DynamicPolyDemo dpd=new DynamicPolyDemo();
Dpd.show();
}
}
We use static variables for storing data or objects globally so that it can be accessed during the life of the application
---------------------------------------------------------------------------------------------------------
Static member of a class is shared by all instances of the class. Here is a simple example:
using System;
class Demo{
public static int k;
public int i;
public void show(){
Console.WriteLine("k={0} i={1}", k, i);
}
}
class Test{
public static void Main(){
Demo a = new Demo();
a.i = 1;
Demo b = new Demo();
b.i = 2;
Demo.k = 4;
a.show();
b.show();
}
}
Output:
k=4 j=1
k=4 j=2
POLY MORPHISM-----
In runtime polymorphism ... the code is called at run time
according to need or given conditions.
suppose there r two methods namely Add() one in super class
and other is in sub class.both have the same name and same
parameters.
so we have to choose that which method from them shld
called at run time i.e. of super class or of sub class.by
polymorphism we do that.
ex:-
class A
{
int add(){//code of the method}
//some other code
}
class B extends A
{
int add(){//code of the method}
//some other code
}
class AB
{
public static void main(String s[])
{
A ob1;
ob1=new A();
int i=ob1.add();//will call the method of super class.
ob1=new B();// sub class's reference can be assigned to
super class address but not vice versa.to do that we have
to type cast the reference of the sub class in reference of
the super class.
int j=ob1.add();//will call the method of sub class
}
}
----------------------------------------------------------------------------------------------------------
No comments:
Post a Comment