• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interfaces Vs abstract classes

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

We have interfaces and abstract classes. whatever we do(declaring methods,defining constants ) in interface can be achived in
abstract class. However when it comes to multiple inhertence we cant use classes thats the reason
we have interfaces in place this is fine.

Now whenever we want to expose the API we always use interfaces eg: in jax-ws,spring etc we always use
interface why cant we use abstract class itself.

Thanks
Chandra
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a sort of best practice. Its called "code to interface". Basically whenever you use such an API, you should know only the contract of the API i.e. what is the role of which interface and what each method does. If you use an interface, you'll now know which class' instance you are getting. So if the underlying class changes your code would not be impacted at all. I'll take an example of HttpServletRequest interface. When you use it in your servlet, you don't know instance of which class you are getting. Tomcat returns you an instance of a different class than Glassfish. The interface is basically the contract defined in Servlet specification which is implemented by Tomcat and Glassfish with their own classes...
 
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another "loose" reason of putting it is the whole concept behind interfaces and abstract classes. An interface is thought of as equipping a class with an ability whereas an abstract class is thought of as associating a class to a particular type.

For example, going back to textbooks, A Ferrari is a type of car with a special ability to doSportyThings. The Car can be an abstract class and the doSportyThings() method can be coded inside an interface called Sportable. This way, the Sportable capabilities can be provided to other Car models on a case by case basis, rather than making Sportable an abstract class which would then make every type of car a sports car.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for the replies, from what i understand we can use either abstract class or interface for exposing
the API's. We can annonate both and provide the same functionality with the help of annotations and reflection
API's. Its is more of best practices than technical reason. Is this correct?

Thanks
Chandra
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Chandra,

It has both the technical reason and best practice. I am sure you must have heard about design patterns. For a clear
understanding of your question, i will suggest that you study a design pattern called Strategy Pattern, it will give you a
clear answer about the confusion. Additionally, programming to interfaces is technically correct also because of maintainability
of code. As what you write today is going to change in future, so as Ankit already described to you.

BR,

 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks i got the concepts. here it goes

We will keep the most generic methods in the interface this is the thumb rule. However we may have to provide
some basic implementation for some of them which rarely be overrided.

In this way we are avoiding duplications of swim() and eat() in all the subclasses. It makes sence
To have interface and abstract classes and this is called programme to an interface not an implementation

now my Question is some thing more specific here. Why interface is a choice over abstract class for exposing APIs

yeah once we expose API through abstract class there is no room for extending another class. Apart from this is there
any other real reason or is this the only reason?

Thanks
Chandra
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

By exposing through abstract classes you are still exposing some of the implementation where as your client
just needs to know the contract. Code duplication is another reason. I will strongly recommend that you go through
Strategy Pattern in Head First Design Pattern by K&B which is the first chapter and you will get a very clear understanding
of why programming through an interface is promoted.

BR,
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic