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?

asked Apr 20, 2014 at 2:41
4
  • 7
    What is an interface? Commented Apr 20, 2014 at 2:42
  • On the second example, you can't do InterfaceA a = new ImplementingClassA(); Commented Apr 20, 2014 at 2:44
  • Here's my favorite question/answer about interfaces. If you read the first answer, you'll be sure to have a better understanding. Commented Apr 20, 2014 at 2: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. Commented Apr 20, 2014 at 3:23

2 Answers 2

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.

answered Apr 20, 2014 at 3:20
Sign up to request clarification or add additional context in comments.

1 Comment

@Zaphyk Now you make an interface for things that can be drunk.
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();
answered Apr 20, 2014 at 2:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.