• Post Reply Bookmark Topic Watch Topic
  • New Topic

programming to an interface  RSS feed

 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
say i have the following:



the thing is we should always program to an interface. now if i wanted to insert a method specific to class B.. would this be a time to redesign? cause all old code would program to anInterface. wats the best way to tackle such a situation?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a new interface:

Then let class B implement this interface. Any client code that needs to use methodSpecificToClassB() will have to use this new interface . Any code which does not need methodSpecificToClassB() can continue using the old one.
[ November 27, 2005: Message edited by: Jim Yingst ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would create a subinterface with the new operation and have B implement that. Implementing more than 1 interface is nasty.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh... yes. Good idea.
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Basically B could just implement "anotherInterface". Cool. thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming to an interface is often a good idea, but beware that you go too far (making your code unnecessarily long and complex) if you make an interface for every single class you write...
 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!