mixm
mixm

Reputation: 531

polymorphism and interfaces

if i have two classes x and y, both extend class w. and x implementing interface z. if i have methods doSomething(w object) and doSomething(x object), what would happen if i call doSomething(x)?

edit: im implementing this on java, more specifically on android. im asking this because some classes which implement a specific interface mostly does the same thing when doSomething() is called. but there are special cases which i would like to single out.

Upvotes: 4

Views: 196

Answers (4)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391306

In C#, the compiler will pick the right method depending on the declared type of the variable, not on the actual type stored in it.

Note, the code below declares W to be a class, and constructs an instance of it. If you make W an interface, and remove its declaration and construction, you'll get the same behavior for x and y as the program below, interface or class for W in this case does not matter.

Let me show you the difference:

using System;

namespace SO2851194
{
    class W { }
    class X : W { }
    class Y : W { }

    class Program
    {
        static void Main()
        {
            W w = new W();
            X x = new X();
            Y y = new Y();

            doSomething(w);
            doSomething(x);
            doSomething(y);
        }

        static void doSomething(W w)
        {
            Console.Out.WriteLine("w");
        }

        static void doSomething(X x)
        {
            Console.Out.WriteLine("x");
        }
    }
}

Here I declare three variables, of type W, X, and Y, and call doSomething passing the three variables, one by one. The output of this program is:

w
x
w

As expected, the compiler will pick the method that has the best fitting parameter type, and in the case of the x variable, it has a method that can take an object of type X.

However, due to class inheritance, we can change the declaration of the variables, but keep the object types constructed, so changing the code like this:

W w = new W();
W x = new X();   // Notice, changed variable type to W
W y = new Y();   // but keep constructing X and Y

This now outputs:

w
w
w

So the fact that the x variable contained an object of type X didn't factor into it, the compiler picked the method from the variable type, not its contents.

In C# 4.0, you now have the dynamic type, so again changing the code to:

dynamic w = new W();
dynamic x = new X();
dynamic y = new Y();

again outputs:

w
x
w

as now the compiler defers picking any method at all until runtime, and at runtime, it sees that the variable named x actually contains an object of type X and then picks the method with the best fitting parameter type.

Upvotes: 1

Simon
Simon

Reputation: 9365

Let's say you have

w object1 = new x();
x object2 = new x();

Passing object1 will execute doSomething(w object) and passing object2 doSomething(x object).

P.S: Of course depending on the language (talked about C#)

P.P.S: Added parameter names to make it clearer

Upvotes: 1

Joseph
Joseph

Reputation: 25513

It depends on the language that you're using.

For instance, in C# it would use doSomething(x object) not doSomething(w object).

However, if you casted it to w then it would use doSomething(w object) like this:

doSomething((w) someObjectOfX);

or

doSomething(someObjectOfX as w);

Upvotes: 2

mhughes
mhughes

Reputation: 630

you cant have two different methods with the same signatures. Its ambiguous code, compilers wont compile and interpreters will throw an error.

Upvotes: -2

Related Questions