Polymorphism and Interfaces

if I have two classes x and y, both are extensions of class w. and x implementing the z interface. if I have doSomething (w object) and doSomething (x object) methods, what happens if I call doSomething (x)?

edit: im implementing this in java, more specifically on Android. im asks about this because some classes that implement a specific interface basically do the same when doSomething () is called. but there are special cases that I would like to highlight.

+4
source share
4 answers

Let's say you have

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

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

PS: Of course, depending on the language (talking about C #)

PPS: Added parameter names to make them clearer

+1
source

It depends on the language you use.

For example, in C # it will use doSomething (x object), not doSomething (w object).

However, if you cast it on w, then it will use doSomething (w object) as follows:

 doSomething((w) someObjectOfX); 

or

 doSomething(someObjectOfX as w); 
+2
source

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

Note that the code below declares W class and builds an instance of it. If you make a W interface and delete its declaration and construction, you get the same behavior for x and y , since the program, interface or class below for W does not matter in this case.

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:

 w x w 

As expected, the compiler will choose the method with the best suitable type of parameter, and in the case of the variable x it has a method that can accept an object of type x .

However, due to the inheritance of the class, we can change the declaration of variables, but save the types of objects, so we change the code as follows:

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

Now it produces:

 w w w 

Therefore, the fact that the variable x containing the object of type x did not take it into account, the compiler chose this method from the type of the variable, and not its contents.

In C # 4.0, you are now of type dynamic , so change the code again to:

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

issues again:

 w x w 

since now the compiler generally refuses to choose any method before execution, and at runtime he sees that a variable with the name x actually contains an object of type x , and then selects a method with the best suitable parameter type.

+1
source

you do not have two different methods with the same signatures. Its ambiguous code, compilers do not compile, and interpreters will cause an error.

-2
source

Source: https://habr.com/ru/post/1310022/


All Articles