File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 42599
    
  65
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: 3071
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why to implement interface