Method / constructor overload with Super / Sub types

I have some questions regarding which overloaded method will be called in certain cases.

Case 1:

public void someMethod(Object obj){
    System.out.println("Object");
}
public void someMethod(InputStream is){
    System.out.println("InputStream");
}
public void someMethod(FilterInputStream fis){
    System.out.println("FilterInputStream");
}

I know that if I pass it String, it will print โ€œObjectโ€. However, if I pass it an InputStream? It gets more confusing if I pass it something like BufferedInputStream. Will it be a call to Object One, InputStream one or FilterInputStream one? Does the order mean that the methods matter?

Case 2:

This is a little trickier because it takes advantage of multiple interface inheritance. Neither BlockingQueue nor Deque are sub / supertypes of each other, but both are supertypes of BlockingDeque. Sun has added multiple inheritance with interfaces because they don't need a tree structure. An ad for BlockingDeque is this public interface BlockingDeque extends BlockingQueue, Deque {.

public void someMethod(BlockingQueue bq){
    System.out.println("BlockingQueue");
}
public void someMethod(Deque bq){
    System.out.println("Deque");
}
public void someCaller(){
     BlockingDeque bd = new LinkedBlockingDeque();
     someMethod(bd);
}

Will this call someMethod (BlockingQueue) or someMethod (Deque)?

Case 3:

You can combine these two with this:

public void someMethod(Queue q){
    //...
}
public void someMethod(Deque q){
    //...
}
public void someMethod(List p){
    //...
}
public void someCaller(){
    someMethod(new LinkedList());
}

Same question: someMethod (Queue), someMethod (Deque) or someMethod (List)?

Case 4:

You can also make things very complex by introducing two arguments:

public void someMethod(Collection c1, List c2){
    //...
}
public void someMethod(List c1, Collection c2){
    //...
}
public void someCaller(){
    someMethod(new ArrayList(), new ArrayList());
}

Will this call someMethod (Collection, List) or vice versa?

Case 5:

It gets worse when they have different return types:

public Class<?> someMethod(BlockingQueue bq){
    return BlockingQueue.class;
}
public String someMethod(Deque bq){
    return "Deque";
}
public void someCaller(){
     BlockingDeque bd = new LinkedBlockingDeque();
     System.out.println(someMethod(bd));
}

. - ? someMethod (BlockingQueue).toString() someMethod (Deque)?

+3
3

, Java , , , , (, InputStream), ( 1 InputStream 2). , downcasting .

, Java . , , , , , , . : , Java, , .

Java, , , . , 4 , , .

5 : Java , , , - , .

, :

  • - InputStream, FilteredInputStream, def, -, InputStream, FilteredInputStream, def, - , 1st def

  • 2nd def

  • ,

  • ,

  • ,

, , , , , , , , , "" , Java , ( ), - . , 4, .

+5

, , . , , , runtime.e.g.

1: InputStream , 2- . BufferedInputStream .

2: , BlockingDeque , ,

case 3: , , Linkedlist

4: , ,

5: . 2 .

+2

This is a bit regarding the issue of overloaded arguments, but there is a pretty clear reason why case 5 is โ€œworse.โ€

Case 5 is where you use language functions called return option types. This was not originally introduced in Java, but was added in version 1.5, I believe (in part because of this problem). If the compiler cannot figure out what the correct return type is, this was unsuccessful, and this happens in this case.

0
source

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


All Articles