Java method that returns interface type

danpakku

As I was reading about java interface, I came across the following example. I know how to implement an interface. But returning interface type in a method is something new to me. Could someone help me to understand this example? Will the methods in the "Animal" interface be implemented in the "categorizeAnimals" method? If yes, the "animals" array will be passed into all the interface methods in the "categorizeAnimals" method?

public class Wild {

  public static interface Animal {
      public List<String> getInvertebrates();
      public List<String> getFishes();
      public List<String> getAmphibians();
      public List<String> getReptiles();
  }


  public static Animal categorizeAnimals(String[] animals) {
    .......
    .......
    .......
    return null;
  }
}

Appreciate your reply, Thanks much.

Kishan Bheemajiyani

very simple solution is that if you have put any interface into the any of the method then then what ever class instance you are returning that class has to have that interface implemented.

public class ImplementedClass implements Interfacename {

    @Override
    public void sayhi(String hello) {
        // TODO Auto-generated method stub

    }

    @Override
    public void sayhello() {
        // TODO Auto-generated method stub

    }

    public Interfacename sayhello1() {
        System.out.println("returning new intance of class which is implementing that interface");
        return new ImplementedClass();
    }
}

interface is

public interface Interfacename {
    public void sayhi(String hello);

    public void sayhello();

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java method that returns interface type

From Dev

Method in Java interface that returns class or subclass type

From Dev

Generic type in interface method Java

From Dev

Calling Type.GetRuntimeMethod on an interface with a generic method returns null

From Dev

Java method parameter type and interface conformance

From Dev

Interface with the method of generic type

From Dev

Java how to implement interface with a variadic method and generic return type

From Dev

Why does a Java method reference with return type match the Consumer interface?

From Dev

Why does a Java method reference with return type match the Consumer interface?

From Dev

Java interface synthetic method generation while narrowing return type

From Dev

Parameterized classes and methods: making a method returns a correct type with java generics

From Dev

Can I override kotlin method which returns Unit type in Java

From Dev

Can I override kotlin method which returns Unit type in Java

From Dev

JAVA: override interface method

From Dev

Default Method in Interface - Java

From Dev

JAVA: override interface method

From Dev

Golang String method for interface type

From Dev

Generic type constraints on an interface method

From Dev

Java method that returns a function?

From Dev

Java Queue of Interface type

From Dev

What type returns the get method?

From Dev

Using an interface method for an object of one data type in an array of a different data type? Java

From Dev

How does one define a generic wrapper interface type and use it as method return type in java?

From Dev

Interface method returns ArrayList containing different objects

From Dev

How to implement interface method that returns Task<T>?

From Dev

How to invoke a method which returns an interface

From Dev

Creating a method that returns an object implementing a generic interface

From Dev

Streamable Interface : How empty() method returns Iterable?

From Dev

Mocking a method which returns Page interface

Related Related

  1. 1

    Java method that returns interface type

  2. 2

    Method in Java interface that returns class or subclass type

  3. 3

    Generic type in interface method Java

  4. 4

    Calling Type.GetRuntimeMethod on an interface with a generic method returns null

  5. 5

    Java method parameter type and interface conformance

  6. 6

    Interface with the method of generic type

  7. 7

    Java how to implement interface with a variadic method and generic return type

  8. 8

    Why does a Java method reference with return type match the Consumer interface?

  9. 9

    Why does a Java method reference with return type match the Consumer interface?

  10. 10

    Java interface synthetic method generation while narrowing return type

  11. 11

    Parameterized classes and methods: making a method returns a correct type with java generics

  12. 12

    Can I override kotlin method which returns Unit type in Java

  13. 13

    Can I override kotlin method which returns Unit type in Java

  14. 14

    JAVA: override interface method

  15. 15

    Default Method in Interface - Java

  16. 16

    JAVA: override interface method

  17. 17

    Golang String method for interface type

  18. 18

    Generic type constraints on an interface method

  19. 19

    Java method that returns a function?

  20. 20

    Java Queue of Interface type

  21. 21

    What type returns the get method?

  22. 22

    Using an interface method for an object of one data type in an array of a different data type? Java

  23. 23

    How does one define a generic wrapper interface type and use it as method return type in java?

  24. 24

    Interface method returns ArrayList containing different objects

  25. 25

    How to implement interface method that returns Task<T>?

  26. 26

    How to invoke a method which returns an interface

  27. 27

    Creating a method that returns an object implementing a generic interface

  28. 28

    Streamable Interface : How empty() method returns Iterable?

  29. 29

    Mocking a method which returns Page interface

HotTag

Archive