-2

First of all, I am aware of the question : What does it mean for a function to return an interface?

But I am still a bit confused about the usage of a method returning a type "interface".

When would I use something like this?

  public Animal saySomething(String str);

Could someone please provide an example of when / where to declare the saySomething() method returning a type interface.

Thanks!

Edit:

My question seems to be confusing to some. So I will try to re-word it. I am asking for an example of how I would use and when I would use a method that returns an interface type. Eventually I found a good example and submitted it as my own answer.

Community
  • 1
  • 1
BustedSanta
  • 1,368
  • 7
  • 28
  • 55
  • 3
    You are better off starting with a problem and saying how would I solve this. If you start with some code and say, can I find a use for this, it often doesn't make any sense. – Peter Lawrey Oct 07 '14 at 19:21
  • 2
    Read about the factory design pattern. – plalx Oct 07 '14 at 19:21
  • is your trouble that you don't understand what is happening? or is it that you don't know why anyone would want o do that? – Sam I am says Reinstate Monica Oct 07 '14 at 19:23
  • @plalx - thanks, very useful comment which eventually led me to find what I was looking for – BustedSanta Oct 08 '14 at 18:16
  • @PeterLawrey - my problem is stated at the top of my question "I am still a bit confused about the usage of a method returning a type 'interface'"... – BustedSanta Oct 08 '14 at 18:17
  • 1
    @Sam I am - mostly, I couldn't picture when/why I would create a method returning an interface type. thank you all for your input... – BustedSanta Oct 08 '14 at 18:17
  • 2
    I suggest you look at the Map interface, it has several examples of method which return interfaces. This is a real example as these methods are widely used. – Peter Lawrey Oct 08 '14 at 20:08
  • You may also find [Why return back or assign to a supertype rather than the implementation type?](http://programmers.stackexchange.com/q/258105/40980) on Programmers.SE to be enlightening. –  Oct 08 '14 at 21:53
  • possible duplicate of [Why should the interface for a Java class be prefered?](http://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-prefered) –  Oct 08 '14 at 21:54

1 Answers1

0

I found a good example of what I was looking for. Compliments of "Cave of Programming"

source: https://www.youtube.com/watch?v=TBAKKulJTJ4

Here's the code.

public interface Animal {

    public void speak();
}

public class Dog implements Animal {

    @Override
    public void speak() {       
        System.out.println("Woof!");        
    }

}
public class Cat implements Animal {

    @Override
    public void speak() {
        System.out.println("Meow!");
    }

}

public abstract class AnimalFactory {

    public static final int CAT = 0;
    public static final int DOG = 1;

    public static Animal createAnimal(int type){

        switch(type){
            case CAT:
                return new Cat();
            case DOG:
                return new Dog();
        }           
        return null;
    }
}


public class App {
    public static void main(String[] args) {

        Animal animal = AnimalFactory.createAnimal(AnimalFactory.CAT);      
        animal.speak();

    }   
}
BustedSanta
  • 1,368
  • 7
  • 28
  • 55