This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why to implement interface" Watch "why to implement interface" New topic
Author

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
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Start here: http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html


Ping & DNS - my free Android networking tools app
Federico Cardelle
Greenhorn

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: 3064
    
  33

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why to implement interface
 
Similar Threads
Why JSP can't implement interface?
Interface Methods
picking variable type from database
usefulness of java interfaces
JUnit Classes quesition?