views:

83

answers:

4

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.

A: 

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

mhughes
The signatures are different. The first is class x and the second is class y. It's a completely legitimate question.
Joseph
+1  A: 

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

Simon
-1: This answer assumes that doSomething(w) and doSomething(x) aren't *both* implemented on x.
Jim G.
w and x are the types, not the variables. Now it should be clear. :)
Simon
@Simon: Duly noted.
Jim G.
@jim G: what happens if they're both implemented on x?
mixm
@mixm: Compiler error, usually.
Simon
@simon: in c#? it works on java btw.
mixm
@mixm: sorry, i was wrong. Of course it works (on C#), too! I would suggest you google "polymorphimsm", "overrding methods", "virtual methods", "overloading methods", "hiding methods". You should be an expert on the topic, after that ;)
Simon
+2  A: 

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);
Joseph
+1  A: 

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.

Lasse V. Karlsen