Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 23 - C# Tutorial - Polymorphism


Part 23 - C# Tutorial - Polymorphism


14 comments:

  1. Hello Venkat,

    I'm bit confused between new(shadow) and override keyword.Kindly explain what's the difference and when to use these.

    Thanks,
    Rohan

    ReplyDelete
    Replies
    1. Got my query resolved...
      The answer is in your next video.
      Thanks...

      Delete
  2. Hi Sir,

    There is a confusion. I have heard that there are two type of polymorphim.

    1.Compile Time
    2.Runtime

    I think the above video explains the runtime polymorphism.
    Kindly explain compile time polymorphism.

    ReplyDelete
  3. I think this is simplier way to explain polymorphism:

    Employee FTE = new FullTimeEmployee();
    FTE.PrintFullName();

    Employee E = new Employee();
    E.PrintFullName();


    ReplyDelete
  4. what are the uses of static key word and how should i use ?

    ReplyDelete
  5. Hi Venkat,

    I have one confusion about run time polymorphism. How one can say that overriding of method happens at run time and overloading at compile time.

    Thanks
    Rakesh

    ReplyDelete
    Replies
    1. as in run time if you choose the reference of base class and the object with new keyword is of child .... and the method u declared in the base class is virtual or abstract , and u have derived it in child class... now the instance u are generating is of type child but the reference is of parent ... so compiler is gonna look parent for the method u defined and then in child the particular method u have defined... it' not easy for compiler for to do so ...so what happens is that at run time which function is to be chosen is decided when the object is passed... this is run time polymorphism

      Delete
  6. contd.

    BaseClass bObj = New ChildClass()
    bObj.commonMethodName

    1> If Base method 'virtual'(Or 'override' in case on multi level inheritance) and child method 'override' -> child method executes
    2> If Base method is not 'virtual'(Or 'override' in case on multi level inheritance) and child method 'new'(optional, vs prompt) -> base method executes (Also called method hiding)

    ReplyDelete
  7. Object reference not set to an instance of an object.
    after execting im getting object reference please help

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;


    namespace ConsoleApplication2
    {


    class Program
    {
    static void Main(string[] args)
    {


    employee[] e1 = new employee[4];
    e1[0] = new employee();
    e1[1] = new parttimemeployee();
    e1[2] = new fulltimeemployee();

    foreach (employee e in e1)
    {
    e.add();

    }
    Console.ReadLine();






    }

    }
    }
    class employee
    {
    public string firstname="sai";
    public string lastname="kumar";
    public virtual void add()
    {
    Console.WriteLine("Base class method called");
    }
    }

    class fulltimeemployee : employee
    {
    public new void add()
    {
    Console.WriteLine(firstname+lastname);
    }

    }

    class parttimemeployee : employee
    {
    public override void add()
    {
    Console.WriteLine("partime employee called");
    }


    }











    ReplyDelete
    Replies
    1. Replace like ...

      foreach (employee emp in e1)
      {
      emp.add();

      }

      Delete
    2. try this
      employee[] e1 = new employee[3];
      e1[0] = new employee();
      e1[1] = new parttimemeployee();
      e1[2] = new fulltimeemployee()

      Delete
  8. "member names cannot be the same as their enclosing type"
    ==========================================================

    using System;


    public class Employee
    {


    public string FirstName = "FN";
    public string LastName = "LN";

    public virtual void PrintFullName()
    {
    Console.WriteLine(FirstName + " " + LastName);
    }


    //public virtual void PartTimeEmployee()
    //{
    // Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
    //}

    //public virtual void FullTimeEmployee()
    //{
    // Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
    //}


    //public virtual void TemporaryTimeEmployee()
    //{
    // Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
    //}
    }

    public class PartTimeEmployee : Employee
    {
    public override void PartTimeEmployee()
    {
    Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
    }
    }
    public class FullTimeEmployee : Employee
    {
    public override void FullTimeEmployee()
    {
    Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee");
    }
    }

    public class TemporaryTimeEmployee : Employee
    {
    public override void TemporaryTimeEmployee()
    {
    Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    //Employee E = new Employee();
    //E.PrintFullName();
    Employee[] employees = new Employee[4];
    employees[0] = new Employee();
    employees[1] = new PartTimeEmployee();
    employees[2] = new FullTimeEmployee();
    employees[3] = new TemporaryTimeEmployee();

    foreach (Employee e in employees)
    {
    e.PrintFullName();
    }
    }
    }

    ReplyDelete
    Replies
    1. There are no methods like PartTimeEmployee(),FullTimeEmployee(),TemporaryTimeEmployee() in base class to override you might have confused while writing..change the method name to PrintFullName() in all those three places
      public class PartTimeEmployee : Employee
      {
      public override void PrintFullName()
      {
      Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
      }
      }
      public class FullTimeEmployee : Employee
      {
      public override void PrintFullName()
      {
      Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee");
      }
      }

      public class TemporaryTimeEmployee : Employee
      {
      public override void PrintFullName()
      {
      Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
      }
      }

      Delete
  9. Hi venkat ,
    I want clear doubt about method hiding and method overriding..Is it correct that method hiding comes in inheritance concept and method overriding comes in polymorphism.please share the answer.

    ReplyDelete

It would be great if you can help share these free resources