1

This may sound like a noob question, But I was searching what are interfaces and I found this example.

Whats the difference between this

interface InterfaceA
{
     void interfaceMethodA();
}


public class ImplementingClassA
    implements InterfaceA
{
    public void interfaceMethodA()
    {
        System.out.println("interfaceA, interfaceMethodA, implementation A");
    }
}

Other than this.

public class ImplementingClassA
{
    public void interfaceMethodA()
    {
        System.out.println("interfaceA, interfaceMethodA, implementation A");
    }
}

I did not find any difference, if so what are used the interfaces for?

Zaphyk
  • 157
  • 2
  • 8
  • 7
    [What is an interface?](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html) – Sotirios Delimanolis Apr 20 '14 at 02:42
  • On the second example, you can't do `InterfaceA a = new ImplementingClassA();` – m0skit0 Apr 20 '14 at 02:44
  • Here's my favorite [question/answer about interfaces](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). If you read the first answer, you'll be sure to have a better understanding. – jahroy Apr 20 '14 at 02:48
  • Your question is way too abstract and we all know abstract is just another term for imaginary land. So I decided to give you an answer that is more realistic. – CodeCamper Apr 20 '14 at 03:23

2 Answers2

1

When you have multiple classes that need to do the same actions but the actions will each be slightly different or even very different then you need an interface. For example lets say you want certain foods to be edible. If you inherit an eating method all the foods will have to be eaten the exact same way. That is why you use an interface like so...

   interface Iedible {
    public void eat();
    }

So by definition if something implements Iedible I know you can eat it but the behavior of how to eat it might be different. For example soup you would have to sip where a cake you would have to bite.

In this case we have a cookie and we all know there is only 5 bites in a cookie.

 public static class Cookie implements Iedible {
    int bites = 5;

    public void eat() {
        if (bites > 0) System.out.println("*bites cookie* you have only " + --bites + " bites left");
        else System.out.println("no more cookie left :(");
    }
    }

Now we have vegetables and we know you never run out of vegetables because of how tiny the bites you take are.

   public static class Vegetable implements Iedible {
    public void eat() {
        System.out.println("Everyone knows vegetables are not edible but you try to eat it anyway *takes a microscopic bite*");
    }
    }

So we see both the Cookie and the Vegetable both are able to eat() but the implementation of those eat() actions are fundamentally different.

Now here is where interfaces really shine. Now I want a class called dinner which can accept ANY object which can be eaten and then I want a method that will allow me to TRY everything on the dinner table.

public static class Dinner {
ArrayList<Iedible> foods = new ArrayList<Iedible>();

public Dinner(Iedible... food) {
    for (int i = food.length - 1; i >= 0; i--)
    foods.add(food[i]);
}

public void tryEverything() {
    for (int i = foods.size() - 1; i >= 0; i--)
    foods.get(i).eat();
}
}

In your example above where you just gave both classes the same method you will be unable to make this dinner class without interfaces because just because both classes have the same method the compiler doesn't know that unless you implement an interface.

And then of course here is our interface in action...

public static void main(String[] args) {
Cookie cookie = new Cookie();
Vegetable vegetable = new Vegetable();
Dinner dinner = new Dinner(cookie, vegetable);
dinner.tryEverything();
}

I hope this helps you gain some practical ideas for interfaces and understand that the example above I just gave will be impossible to replicate without interfaces.

CodeCamper
  • 6,609
  • 6
  • 44
  • 94
0

It's part of a contract in the first instance, consider another implementation of the interface -

public class ImplementingClassB
    implements InterfaceA
{
    public void interfaceMethodA()
    {
      System.out.println("interfaceA, interfaceMethodA, implementation B");
    }
}

now you could use

InterfaceA foo = new ImplementingClassA();
InterfaceA bar = new ImplementingClassB();

foo.interfaceMethodA();
// and/or
bar.interfaceMethodA();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249