Using the interface as a type

I was looking at some kind of production code, and I was a bit confused about the concept of using an interface as a type (object)

I saw this explanation http://download.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

Here is the code below:

public Object findLargest(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ( (obj1).isLargerThan(obj2) > 0)
      return object1;
   else 
      return object2;
}

Where Relatableis the interface, I don’t understand where it gets the logic for calling the function isLargerThan(obj2).

Can anyone understand this for me?

+3
source share
3 answers

, . , . , , , , .

+8

, , ; ClassCastException .

+2

obj1 Relatable, , Relatable. , , . .

+2

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


All Articles