Reputation: 155
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?
Upvotes: 1
Views: 333
Reputation: 6984
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.
Upvotes: 1
Reputation: 201527
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();
Upvotes: 0