-1

For Java!! We can implement different methods in different classes. In interface we create an abstract method and provide its implementation in the class that implements the particular interface. What is the purpose of creating an interface if we can create and implement methods in classes separately? Plz help me I'm new in Java?

user4736473
  • 23
  • 2
  • 4
  • Many classes (Vector, ArrayList, LinkedList) all implement the **same** interface - List. – Jan Jan 14 '16 at 11:42
  • You should read a good Java book or tutorial. Google has thousands of pages explaining interfaces. – callmebob Jan 14 '16 at 11:50

4 Answers4

1

Consider an example :

Suppose you are creating some application which is concerned with the animal kingdom.

Now you are asked to create a dog, cat, lion etc objects.

So first thing will come to your mind, since these all belong to animal kingdom I can create a base class named ' Animal ', and everything will inherits it. Now yo created something like this

class Animal {    
legs;    
family; 

eat();    
roam();  
sleep();     
makeNoise();    
}

So all the animals inheriting the animal class will have these features. You can call this as a "IS-A" relationship. Like Dog IS-A Animal.

Now suppose you are asked to use your animal simulation program for some science-fair. You can use this design in that too.

Now suppose someone asked you to use your simulator in a pet-shop.

Since you don't have any pet behavior. What you did is add the pet features in the base class and thought this will work.

So now you program can create a lion which has the pet behavior. STRANGE!!

Now what you need to put all the pet behavior in one place and make sure that all the pet animals should posses it.

A way to do is create another superclass with all pet features and extend it. This is multiple inheritance, which JAVA don't allow (just Google deadly diamond of death). So comes the interface.

Interface is more like a set of behaviors which your object implements. And since every object can have its own set of implementations, all these methods should be abstract.
It gives you polymorphic benefits without deadly diamond of death problem. It is a more like a contract which defines that your object must implements following features.

So now what you can do

interface PetBehavior{    
  befriend();    
  play();    
}  

and classes from different inheritance tree can implement this interface.

mykpc
  • 37
  • 1
0

We do that in order to organize data. This the ability to perform operations lots of times and to structure your data. There is another thing called Vector. If objects implement the same method, they can be iterated and sorted via this Vector

Kyle Luke
  • 348
  • 5
  • 16
0

Because with a single interface you can have multiple implementation. For example if you have a list interface, implementation could be ArrayList or LinkedList. They have different performances and are used based on the context. By having the same interface, if you want to change something you have to simply change from this

List<String> arrayList = new ArrayList<String>();

to this

List<String> arrayList = new LinkedList<String>();

bacause they have the same methods but implemented in different ways

Chris
  • 1,692
  • 2
  • 17
  • 21
0

I used to have this question too!

There are 3 main reasons why we need interfaces:

  • it makes more sense. Interfaces create a "can be used as" or "have the ability to" relationship between the implementing class and the interface. For example, you can have an interface called Flyable and all the things that can fly implements this interface. E.g. Bird, airplane and balloons. These implementing classes "have the ability to fly" because they all have a fly method or whatever is defined in the interface. Also, if a method requires a Flyable object, the programmer can just pass in a bird, an airplane, etc. with his/her common sense. But still, this isn't very practical, just like whether you should write the { on a new line doesn't matter.

  • It makes it easier for you, and other programmers using your API or library or whatever you are creating, to create custom behaviors. Java Swing is a very good example. If you don't know what swing is, it is an API used to create programs that has a "window" or GUI. How are you going to tell the computer what to do when the user clicks on a button? Via an interface! There is an interface called ActionListener. It let you create your own things to do when the users clicks on a button. You just pass an ActionListener object to a method, with your own implementation, and it will be run when the button is clicked! If my words don't make sense, let me use one sentence to summarize all this.

    interfaces provide a way to pass around different methods (with custom implementations) as parameters in methods.

EDIT

Oh I missed one point!

  • interfaces aid polymorphism. Say you have a dog, a cat, and a fish class and all of them don't implement any interfaces but have similar methods (move, sleep, eat etc). If you want to create an array of all your animals, you can only create an array of Object because all Java classes inherits Object. This is unsafe because then you can add whatever you want into the array, and you need to do casting in order to use those move sleep eat methods. So sad! :( If you create an interface called Animal which contain the three common methods and make all three classes implement it, you can just create an array of Animal. And you don't even need to cast it to the right type before you can access move sleep eat! How cool is that!

So remember to create interfaces when a lot of your classes have similar methods but different implementations to just UNITE THEM ALL!

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313