• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

why to implement interface  RSS feed

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Why to implement interface when we can write all that method in class without implementing interface?
 
Rancher
Posts: 42974
76
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!