-3

I have been wondering the real use of interface. please check the below code.

interface Animal {
    public void eat();
    public void travel();
}


public class MammalInt implements Animal{

    public void eat(){
        System.out.println("Mammal eats");
    }

    public void travel(){
        System.out.println("Mammal travels");
    } 

    public int noOfLegs(){
        return 0;
    }

    public static void main(String args[]){
        MammalInt m = new MammalInt();
        m.eat();
        m.travel();
    }
} 

in the above code if i remove implements Animal from class declaration still the code works fine without any difference. So what is the actual use of interface. ?

awksp
  • 11,764
  • 4
  • 37
  • 44
  • Please atleast do 5 mins of research – opensource-developer May 14 '14 at 06:04
  • 1
    Have you read the rules before posting the question? This question has been asked a million times and I'm sure if you write it on google first link would give you the information you need – Alexandru Severin May 14 '14 at 06:07
  • @user3635242 It's true people are being hard on you, perhaps a _tad_ more than they should. But what google searches did you try? This question has been asked and answered many times already, and you can find some of those answers with variants of this question's subject line. Since you haven't referred to any of those, it makes people suspect you haven't tried very hard to find them on your own. – yshavit May 14 '14 at 06:08
  • I googled alot but my question is different here, pls see it carefully, I am asking why interface because in my above class even if i remove implements still the code works – user3635242 May 14 '14 at 06:09
  • 3
    Your question actually isn't different than the others. Please read some of the links that have been mentioned above; they all ask why one would want to use an interface when it's not strictly needed, and answers are given. Also note that not _every_ class needs an interface; so if your question is "why do I need one in this specific case, assuming that this is the whole program" -- then the answer is that you don't. But in reality programs are much more complicated, and thus benefit from the gains that interfaces give, as mentioned in those other links. – yshavit May 14 '14 at 06:12
  • Above code works if you remove `implements Animal`. Sure thing. But it won't work or compile if you remove/comment out those two `eat()` and `travel()` methods. Even if you remove call to those two methods from `main()`. And that's basic principle of interface. Just read some stuff posted above. – SachinGutte May 14 '14 at 06:14
  • `if i remove implements Animal from class declaration still the code works fine` Okay...But _What made you think that it should not work?_ – akash May 14 '14 at 06:16
  • I have commented those 2 method inside the interface but for my surprise it still worked. how???? – user3635242 May 14 '14 at 06:18
  • 1
    Dont comments those methods from interface. Comment out their definition in you class. – SachinGutte May 14 '14 at 06:20
  • @SachinG it should not work because why to use implement then, if its of no use then why? – user3635242 May 14 '14 at 06:20
  • `I have commented those 2 method inside the interface but for my surprise it still worked` okay it will because you have implemented interface with _NoMethods_.So you don't need to put any method implementation in your class. – akash May 14 '14 at 06:21
  • Think of it as this way a real life example of above class case. You have a mammal. Mammal neads to `eat` to live and mammal needs to `travel` to loosen up fat. So these two things a mammal must do. Basically this is the case. Interface restricts you to define methods which a class must define. If it does not then it'll not compile. Does not care if you actually call them or not from you main. But they must be defined in your class. – SachinGutte May 14 '14 at 06:26

2 Answers2

1

The purpose of an interface is not to do things but to define what things the class implementing the interface must do. In your case the interface Animal defines what functions can be invoked on an Animal. So any functions/Classes that rely on the Animal interface know what functions will be implemented by the class implementing the interface Animal.

For eg: suppose there is a method that makes the Animal travel , eg :

 public void goToDestination(Animal a)
 {
     a.travel();
 }

Now this function can call the function travel on the Object of type implementing Animal without knowing what is the exact type of the object (It can be a MammalInt or it can be a Amphibian etc). This is the power of interfaces. This is only a basic layman explanation of interface you can Google and read more about Interfaces.

Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • The purpose of an interface is not to do things but to define what things the class implementing the interface "must" (not "can") do. – TheLostMind May 14 '14 at 06:12
  • @WhoAmI Thanks a lot for pointing out !! – Kakarot May 14 '14 at 06:13
  • @WhoAmI I think `can` is better phrasing... The object can invoke its methods. The class must implement them, but the interface does define what the object of a class which implements that interface `can` do, not what it `must` do. it's not a requirement that you call every method that an object has an implementation of. – Paul May 14 '14 at 06:14
  • @YTowOnt9 - ya.. You are right. What the class "must" support (be able to do). kakarot - My apologies :) – TheLostMind May 14 '14 at 06:16
1

No doubt your code works even without the interface at this moment, but. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

For example, if you have multiple animal classes, each implementing Animal, if later on you want to change the structure of the Animal interface, it helps you to easier change the structure of all your animal calsses.

Another example, lets say you work on a group object, and the leader tells you and someone else to make some classes based on a structure given by him (the interface), implementing the interface asures that you got all the method names and structures right. If later on the leader decides that the structure of the classes isn't good, he will change the interface, forcing you to change your class.

For more information, read What is an interface or Undersanding interfaces and their Usefullness

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
  • thanks..i understand completely now..u banged on the point... :) loads of love from several years younger than you.:) – user3635242 May 14 '14 at 06:32