1

I'm working on a project which has some legacy code and I'm not sure how certain interfaces interact with each other.

There are two interfaces, that look something like this:

public interface FirstInterface {
     void someFunction();
}

And the second one which calls (?) the first one:

public interface SecondInterface {
     FirstInterface fi();
}

I'm new to Java and I don't understand if this is some form of nested interface or if something else is going on here. I'm also not sure if FirstInterface fi() calls the interface or creates a method of that type and if so what is the advantage over doing something like FirstInterface fi? If it helps, both these interfaces are in different packages.

If I can add any information to make this clearer, please let me know and thank you!

Projjol
  • 1,195
  • 2
  • 12
  • 26
  • 6
    `FirstInterface fi();` is defining a method `fi` that returns an object of type `FirstInterface` – Smutje Jul 21 '20 at 13:22
  • thanks for your comment @Smutje, I have a follow-up and this probably varies from case to case but what is the advantage of having a method with an interface as the return type instead of something like implementing the interface and accessing any internal function? – Projjol Jul 21 '20 at 13:32
  • Regarding follow-up: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947) – Pshemo Jul 21 '20 at 13:36
  • The fact the return type is derived from an interface isn't necessarily relevant. It is a return type just like anything else. If the interface had a method `int getInt();` that would seem normal. `Object getObject();` ? `Runnable getRunnable();` where Runnable is also an interface. – matt Jul 21 '20 at 14:03

2 Answers2

2

Interfaces need to be implemented. So there will be a class, say FirstClass, defined something like this:

class FirstClass implements FirstInterface {
    public void someFunction() {
        System.out.println("Hello, world!");
    }
}

and another class that implements SecondInterface. Notice from the syntax that the method fi() returns an instance of FirstInterface; it does not call it. In fact, you can't call an interface (or a class): you can only call methods.

To return an instance of FirstInterface, the fi() method must create an object of a class that implements it (such as FirstClass), and return that object. As an exercise, you might want to try writing a class SecondClass and its fi() method.

k314159
  • 5,051
  • 10
  • 32
1

Interfaces in java are used for polymorphism. The key point is that interfaces are only definitions not implementations. These declarations are also called method signatures as they declares the name, return-type and parameters of the method.

In your code FirstInterface only declares a method which doesn't return anything. On the other hand SecondInterface declares a method which returns an object of type FirstInterface. But as I said, these are only declarations. These interfaces are implemented by Classes where we define their body. I think nothing is better than a code snippet. So consider this example:

public interface MyInterface{
    void meathod1();
    int meathod2();
    String meathod3();
}

public interface MyInterface2{
    MyInterface meathod4();
}

public class Class A implements MyInterface{
    public void meathod1(){     
    } 

    public Integer meathod2(){
        return 0;       
    }

    public String meathod3(){       
        return "Apple";
    }
}

public class Class B implements MyInterface{
    public void meathod1(){     
    } 

    public Integer meathod2(){
        return 1;       
    }

    public String meathod3(){       
        return "Banana";
    }
}

public class Class C implements MyInterface2{
    public MyInterface meathod4(){
        MyInterface myInterface = new A();
        return myInterface;
    }
}

Here MyInterface and MyInterface2 are 2 interfaces. First one declares 3 methods which return nothing, integer and String respectively. Second interface declares method which returns object of type MyInterface.

MyInterface is implemented by 2 classes namely A and B. Both of them assign definitions to the methods as per their needs.

MyInterface2 is implemented by Class C, which creates an reference myInterface of type MyInterface. Because MyInterface was implemented by classes A and B, it can point to object of either of them. In the code above we made an object from class A and associate it with myInterface. Finally we return it as specified by the method signature. This is similar to what happening in your code. :)

ngock
  • 25
  • 4
gpl
  • 1,380
  • 2
  • 8
  • 24