-6

I am just in little bit confusion about for what interface used in Java. For example, I am creating an interface like,

interface Inter
{
   void get();
}

And I am implementing it in a class like,

class Base implements Inter
{
   void get()
   {
     ------
   }
}

Whats the difference between, if I declare a class like

class Base
{
   void get()
   {
     ------
   }
}

Is there any difference in it? then why should I use interface in java. I know its a basic question. but I am in confusion. So please solve this..

sexp1stol
  • 445
  • 4
  • 17
A.Mohamed Bilal
  • 115
  • 1
  • 13
  • the following may help you [http://stackoverflow.com/questions/4436087/what-is-the-actual-use-of-interface-in-java][1] [1]: http://stackoverflow.com/questions/4436087/what-is-the-actual-use-of-interface-in-java – sareeshmnair Feb 19 '14 at 10:47
  • use your best friend google for that – lakshman Feb 19 '14 at 10:50
  • The joke with these kind of questions is that the answer becomes more clear when you start to actually NEED interfaces, which happens when your application design requirements become slightly less-than-trivial. – Gimby Feb 19 '14 at 10:52

4 Answers4

2

An interface makes the contract between caller and called classes more explicit.
Also, it allows you to mimic multiple inheritance to a certain extent (say you
have super class A and you cannot change it, you can still implement some
interface B and pass your class to a method which accepts B as parameter
but knows nothing about your super-class A).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1
public interface Animal {

public void eat();
public void sleep();

}

public class Dog implements Animal{

// Now FOR A DOG TO BE AN ANIMAL, IT MUST IMPLEMENT ALL THE BEHAVIOURS(METHODS) OF ANIMAL INTERFACE. IF IT MISSES EVEN ONE BEHAVIOUR, THEN A DOG IS NOT AN ANIMAL.
public void eat()
{
}
public void sleep()
{
}
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Why Interfaces?

An interface is a contract or a protocol, or a common understanding of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation.

One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.

Nidheesh
  • 4,390
  • 29
  • 87
  • 150
0

First, it can be usefull because you can't herit from multiple class.

And then it is a kind of contract also.

All classes which implements Movable will be able to move, but each one with his own way. So when you handle a Movable, no matter his class, you know that you can use the method move().

Hope it helps.

Xavier Haennig
  • 308
  • 1
  • 12