-2

Possible Duplicate:
Java Interfaces?

I am not the newest to java but there is one thing in it that I could not understand till date that what is exactly the purpose solved by an interface? I know that in order to attach the multiple inheritance with java it can be used but that should not be the soul reason. And most of the other goals of an interface can be served by the abstract class. Even the books do not give a sufficient answer.

Community
  • 1
  • 1
Nitish Pareek
  • 2,453
  • 3
  • 19
  • 18

5 Answers5

1

Interfaces don't provide multiple inheritance on classes. Although interfaces can extend multiple other interfaces.

An interface is akin to a contract. For example you are a class of object called Human. You then have mutliple contracts to fufil that are not generic to all Human objects. Such as your paying your mobile phone contract, making dinner for the wife, etc.

Interfaces just tell you what the object is contracted to do for you. Where as as Class defines it.

Hope that helps.

Dan
  • 1,030
  • 5
  • 12
1

I know that in order to attach the multiple inheritance with java it can be used but that should not be the soul reason

Why isn't the fact that you can have multiple interface inheritence not an really important solution for you, you don't explain.
Besides this really important fact (that helps Java not to have the diamond problem present in C++) - which you consider as no big deal- it also models a different kind of relationship among your objects.
An abstract class would be used to model an is-a relationship. With the interface you model a has-a relationship capturing composition and you additionally have the ability to combine common functionality among classes that would otherwise be unrelated to each other.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
0

To Define Type Interfaces are best candidate. Read Chapter 4-Classes And Interfaces from Effective Java.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
0

I'll give you a small example for the use of interface.

I have a class Company that contains an ArrayList<Employee> employees and a function giveRaise() that gives a raise to the employees by adding their salaries.

Now imagine that instead of Employee, i have three classes Manager,SalesPerson, Developer,
they all get a raise but each one differently.

You think you can create a superClass called Employee, but you don't want to make the same method giveRaise(), and you don't want to have a default treatement for giveRaise().

So what you need to do is everytime you create a class(as in developer writes a new class) that inherits from Employee you want to oblige it to implement the function giveRaise and define it.

This is where the Interface comes handy. Of course you can use an abstract class. but an with an interface you can still inherit from another class.

Dany Y
  • 6,833
  • 6
  • 46
  • 83
0

As it is the greatest advantage of interfaces in generell, you can develop parts of a program more autonomous, since over a interface you have some sort of contract what needs to be implemented, and on what you can rely on that it will be there for you to use (in the other parts of the program).

nurgan
  • 309
  • 1
  • 4
  • 22
  • sorry for this quite redundant answer, due to my bad internetconnection i didnt get the notice that another answer has been posted... – nurgan Aug 22 '12 at 10:47