0

I have created two objects of Manager class in Main() as below:

Manager mgr = new Manager();
Employee emp= new Manager();

What I theoretically understand is that 1st object creation [mgr] is compile time binding whereas 2nd object creation [emp] is run time binding. But I want to understand it practically what actually happens that decides that function call will be binded to Function name at compile time [in my case, mgr] or run time [in my case, emp].

What I understand here is that, in both these situations objects are to be created at run time only. If I say new Manager() then it has to create object of Manager only. So, Please suggest what actually happens at run time that is not the case with compile time.

namespace EarlyNLateBinding
{
    class Employee
    {        
        public virtual double CalculateSalary(double basic, double hra, double da)
        {
            return basic + hra + da;
        }
    }

    class Manager:Employee
    {
        double allowances = 4000;
        public override double CalculateSalary(double basic, double hra, double da)
        {
            return basic + hra + da+allowances;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp= new Manager();
            double empsalary = emp.CalculateSalary(35000, 27000, 5000);
            Console.WriteLine(empsalary.ToString());

            Manager mgr = new Manager();
            double mgrsalary = mgr.CalculateSalary(35000, 27000, 5000);
            Console.WriteLine(mgrsalary.ToString());
            Console.Read();
        }
    }
}
WpfBee
  • 2,837
  • 6
  • 23
  • 29
  • 2
    This question appears to be off-topic because it makes no sense. There is no such thing as early or late binding in C# creation of objects – Mitch Wheat Sep 18 '13 at 04:40
  • @Mitch: I am sorry, it's my mistake. I mean the difference between compile time polymorphism and run time polymorphism. Please read compile time binding as compile time polymorphism, and run time binding as run time polymorphism. – WpfBee Sep 18 '13 at 05:16

1 Answers1

1

Both cases are examples of early binding since it's 100% deterministic at compile time how to instantiate the objects. There is no dynamic behavior here. Late binding occurs when it can't be determined at compile time how to instantiate an object. A typical example of this is reflection.

TGH
  • 38,769
  • 12
  • 102
  • 135
  • If i do *if (DateTime.Day == Tuesday) oEmp = Manager else oEmp = Employee* – Andyz Smith Sep 18 '13 at 04:52
  • then it is still early bound, the compiler always knows one or the other ma happen. But at runtime, a vtable is used to actually resolve the call to oEmp.CalculateSalary. which is a runtime vtable, but not latebinding? – Andyz Smith Sep 18 '13 at 04:54
  • Here is a discussion on the toptic http://stackoverflow.com/questions/484214/early-and-late-binding – TGH Sep 18 '13 at 05:00
  • but it can't predict which method to call. which kinda defines what an object **is** by it's behavior. so i'd say the object oEmp is late defined, but that's seem quite different than late bound. The table in the linked post has the salient factors smmarized nicely. – Andyz Smith Sep 18 '13 at 05:02
  • That doesn't imply late binding though. Late binding deals with discovering how to instantiate the object. That's not the case here since it can be determined by the compiler ahead of time. It doesn't change anything that you have branches in your code that instantiates different objects under certain condition. It's all about knowing ahead of time how to instantiate the object when it's requested by the program – TGH Sep 18 '13 at 05:07
  • Guys Sorry, somewhere I read that compile time polymorphism is early binding whereas run time polymorphism is late binding. I wanted to ask difference in between compile time polymorphism and run time polymorphism. Please suggest in terms of polymorphism concepts. During current discussion, I am also getting one query. With the code I posted above, if I wish to call base class method from derived class object then I am not able to do so. Can you suggest how to call base class method here with derived class object? – WpfBee Sep 18 '13 at 05:38