You are on page 1of 3

Polymorphism:-Polymorphism came from 2 greek words poly and morphos

Poly means meany and morphos means forms


simply polymorphism means anything that exist in multiple forms
in object oriented programming languages like C#.net or Java we can achive
Polymorphism in 2 ways:-
1. OverLoading
2. OverRiding
OverLoading:-it is a process of defyning multiple methods with same methodname and
with different Parameters is called as Method OverLoading
which method will gets executed was decided based on the values that we pass
Ex:-
class Console
{
public static void WriteLine()
{ }
public static void WriteLine(int x)
{ }
public static void WriteLine(string x)
{ }
public static void WriteLine(double x)
{ }
19 methods
}
Console.WriteLine();
Console.WriteLine(10);
Console.WriteLine("sathya");
Console.WriteLine(2.3);
Q)Does Overloading is possible with inheritance?
class A class Test
{ {
public void Add(int x,int y) static void Main()
{ {
Console.WriteLine(x+y); B b1=new B();
} b1.Add(5,3);
public void Add(int x,int y,int z) b1.Add(5,4,3);
{ b1.Add("ab","cd");
Console.WriteLine(x+y+z); }
} }
}
class B:A
{
public void Add(string x,string y)
{
Console.WriteLine(x+y);
}
}
Constructor OverLoading:-it is a process of Defyning multiple constructors with
same name and
with different parameters
Which constructor will gets executed was decided based on the values that we pass
at the time of
Object creation
Overriding:-it is a process of defyning Multiple methods with same method heading
and with Different Method body in Base class and Derived class
it is a process of ReImplementing base class method in Derived class
Method:- Method is a subprogram which is used to perform a specific operation
Different Types of Methods are:-
1. Concrete Method:- Method with Body is called as Concrete Method
2. abstract Method :-Method without Body is called as Abstract Method
Q)what is Reimplemented Method?
Modifying the super class method logic in sub class
Rules to be followed while applying Overriding:-
1. Same Method Heading and Different Method Body in base class and Derived class
2. Inheritance is Manadatory
3. Base class Method must decorate with virtual keyword and derived class method
must
decorate with override keyword
4. Always in Overriding overriden method will gets executed irrespective of
Reference

class A class Test


{ {
public virtual void Show() static void Main()
{ {
C.WL("i am A show"); B b1=new B();
} b1.Show();
}
class B:A
{ }
public override void Show() }
{
C.WL("i am B show");
}
}

======================================================================
Q)what is virtual method?
virtual method must decoarate with virtual keyword
virtual method is eligible to override
Q)is it manadatory to overridie virtual method?
Overriding virtual method is optional in Derived class
Q)what is the difference between abstract method and virtual Method?
abstract method virtual method
1. abstract method must declare with 1. virtual method must declare with
virtual
abstract keyword keyword
2. abstract method is unimplemented 2. virtual method is implemented method
method
3. it is manadatory to override 3. overriding virtual method is
optional in
abstract method derived class

================================================
Q)what is Upcasting?
subclass object assigned to super class Reference
in the scenario where single parent and multiple child exist then reference is
fixed
i.e reference is created for super class and object was created for sub class
class RBI
{
public virtual void ROI()
{
ROI is 3/-
}
}
class ICICI:RBI class Axis:RBI class SBI:RBI
{ { {
public override void ROI() public override void ROI()
{ {
ROI is 5/- ROI is 7/-
} }
} } }
RBI rb=new ------();
---- may be ICICI,Axis,SBI
rb.ROI();
in appn development if single parent and multiple child exist then object was
created
for derived class by server,CLR,Container and that object was assigned to super
class
Reference
Q)what is Compiletime Polymorphism?
Method call will bind with Method Behavior at compiletime i.e which method
will gets executed was decided at compiletime
we can achive Compiletime Polymorphism by using Overloading
Q)what is Runtime Polymorphism?
Method call will bind with Method Behavior at Runtime
i.e which method will gets executed was decided at runtime based on the object
creation

You might also like