This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why to implement interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why to implement interface" Watch "why to implement interface" New topic

why to implement interface

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi All,

Why to implement interface when we can write all that method in class without implementing interface?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Start here:
Federico Cardelle

Joined: Jul 26, 2011
Posts: 26
A method can ask in its signature, as a parameter, for a object that implements an interface, regardless of its concrete class. So you can pass to that method an instance of any class that implements the interface.
The method knows that it can invoke on that object any of the methods declared by the interface.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3151

If you have all the methods in a single class then you would be violating the - Single Responsibility principle which states that the classes should be modifiable for no more than one reason- that is it should have single responsibility. Basically its just so Non OO Design.
You want re-usability of the methods in the interface and moreover Java doesn't support multiple inheritance that means you cannot keep extending multiple classes.

Mohamed Sanaulla | My Blog
I agree. Here's the link:
subject: why to implement interface
It's not a secret anymore!