[Logo]
Forums Register Login
why to implement interface
Hi All,

Why to implement interface when we can write all that method in class without implementing interface?
Start here: http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html
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.
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.
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS


This thread has been viewed 796 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 21, 2018 15:50:03.