Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism and overloading with static methods in C#.

I have been trying to generate a Factory supposed to return a different object of a common interface (say Item) according to the input parameter (I call it a context) of the function getItem(A context)

Now, assume I define a new type of context: B which inherits from A.

I wanted to return a different item depending on whether the object passed to the factory was of class B or A.

I tried to do as follows (overloading the method):

class Factory
{
   static Item getItem(A context) {...}
   static Item getItem(B context) {...}
}

This works fine if I do something like this:

B bContext=new B();
Item it=Factory.getItem(bContext);

However, if I cast and object to type A:

A bContext=(A) new B();
Item it=Factory.getItem(bContext);

the first factory method is called.

I thought that polymorphism would ensure the execution of the second method even after the cast, and I would like to know if I missed something?

I am aware that I could keep having a single method and use the is operator to check what the type of the variable is, but I thought the solution I presented above was a bit more elegant.

like image 897
SRKX Avatar asked Jul 28 '11 12:07

SRKX


People also ask

Does polymorphism work on static methods?

I know that polymorphism does not work with static methods, only to instance methods. And also that overriding doesn't work for static methods. Because c calls the m1 method, but it's static, so it can't override and it calls the method in class Mini instead of Car.

Can the static methods be overloaded?

Can we override a static method? No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time.

Is operator overloading static polymorphism?

Operator overloading is an example of static polymorphism. You can leverage operator overloading or to add functionality to operators so as to work with user defined types much the same way you work with fundamental data types.

Is overloading and overriding static polymorphism?

Overloading and overriding are two types of Polymorphism. Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism.


1 Answers

Overloading is decided at compile-time (aside from using dynamic typing in C# 4) based on the compile-time type of the arguments - and in your last snippet, the compile-time type of the argument is A, so it calls Factory.getItem(A).

Only virtual method calls are polymorphic (using overriding), where the actual execution-time type of the target object to decide which implementation to call. If it makes sense for A and B to have a virtual method (overridden in B) which can be called by Factory.getItem to handle the differences, that's great... otherwise you're stuck with either dynamic typing or something like is.

like image 126
Jon Skeet Avatar answered Oct 08 '22 06:10

Jon Skeet