English speaking and learning Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, March 4, 2017

Polymorphism in OOPS

Polymorphism is a Greek word that means "many-shaped" .Polymorphism is one of the fundamental concepts of OOPS.
Polymorphism means one name many forms. Polymorphism means one object behaving as multiple forms. One function behaves in different forms. In other words, "Many forms of a single object is called Polymorphism."

  • Person behaves as a SON in house, at the same time that person behaves like an EMPLOYEE in the office.
  • Mr X behaves as a Boss in office  and behaves father,husband in Home.
There are two types of polymorphism:
  1. Static or compile time polymorphism
  2. Dynamic or runtime polymorphism
First I go with runtime polymorphism ,then easy one compile time polymorphism.

Dynamic or run time polymorphism 
Run time polymorphism means we want to have a association.we are searching same behavior in different person.
There is a probability of incoming feature means class is  searching same kind or better behavior, same name of function in multiple classes.

To achieve run time polymorphism,we need more than one class and one function should be common.
inheritance is must for run time polymorphism.

If there is no run time polymorphism,there is no redefinition,no reproductivity,no overriding of derived class.
If we don't go for run time polymorphism,how can we go for redefining of function of derived class as we can not modify function of base class.
In simple words, If we want to do redefinition of function of derived class,we have to go for run time polymorphism.

for run time polymorphism,there should be
b)One function must be common.
c)if derived class override function of base class, base class has to check overridden function.
d)base class will make object of derived class and pass reference to base class.
e)base class only check overridden function.

Base class cannot call function of derived class directly  so to call function of derived class from base class, we make that function as virtual  in base class.

The parent class uses the virtual keyword for method. Every class that overrides the virtual method will use the override keyword.

Virtual methods allow subclasses to provide their own implementation of that method using the override keyword.;Virtual methods can’t be declared as private.

when base class creates object of derived class, virtual function creates a virtual memory for derived class not base class.virtual function creates virtual table and it contains those function that were overridden.

Override Keyword: method overriding is modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.

Step 1- There is a class "father". "Father" has 2 function - Education and salary.
Step 2 - I have make Education function as virtual.      

      class father
            public virtual void Education()
                Console.WriteLine("I am graduate");
            public void Salary()
                Console.WriteLine("Father salary is 1000");

Step 3- There is a class "son". "son" class is inheriting "father" class. As "father" class has one virtual function Education, we are overriding it in derived class and changing its definition.
class son : father


            public override void Education()


                Console.WriteLine("I am Post graduate");

            public new void Salary()
                Console.WriteLine("Son salary is 2000");

Step 4- we are craeting object of son class and passing its reference to father class.
static void Main(string[] args)


            father f = new son();


Step 5- when we run the program, output is

I am Post graduate    ---value from son class  as we have changed definition in son or  derived class.
Father salary is 1000  --- value form father or base class 


Static or compile time polymorphism
Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. 
Compile time Polymorphism is also known as method overloading.
 Method overloading means having two or more methods with the same name but with different signatures.
Advantage of this  is execution will be fast. Because every thing about the method is known to compiler during compilation it self and disadvantage is lack of flexibility.

class Class1
        public class Test
            public void Add(string a1, string a2)
                Console.WriteLine("Adding Two String :" + a1 + a2);
            public void Add(int a1, int a2)
                int sum = a1 + a2;
                Console.WriteLine("sum of Two Integer " + sum);

        static void Main(string[] args)
            Test obj = new Test();
            obj.Add("Csharp ", "LearningSquare");
            obj.Add(5, 10);

You also like this.

No comments :

Post a Comment