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.
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.
Comments