Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Disagreement with "Coding to an Interface" example : Voilate the basic usage of interface  RSS feed

shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that interface help us in providing a polymorphic behavior but so does the abstract class then why is there a need for interface? By Object Oriented Approach Abstract classes are related to its Implementing classes (Subclasses) by means of Specialization or Generalization but interfaces fundamentally are not meant for specialization or generalization.

The question arises then why do we need an interface? how do we know when to use an interface ?

ans : In an application where there are say hundreds of domain classes each with its specific inheritance hierarchy, say several of the classes reuire a small functionality for adding color to itself i.e. they are Colorable. So i will make an interface by name Colorable. Now any class that implements colorable can change its color. Here we gether that interfaces must be used only when we wish to add a particular property in a specialized way to several classes.

Now when i say Vehical - it is a mode of trasport. but cant create an instace of a vehical as we dont go to shop to ask for a Vehical rather we go asking for a Car, Bike, Bicycle etc. So for such an application Car, Bike, Bicycle are all Specialization or in other words they are Type of Vehicals.

but when i implement Colorable to these classes they can be colored or probably their color can be changed within the application. And it certainly does not mean that Car, Bike, Bicycle are all Type of Colorable (Specialization).

The example on page 224 : Coding to an interface suggests a wrong example in that regards, voilates Object Oriented Paradigm. I think Kathy and Bert would certainly agree with this since it comes from thier book and i highly regard kathy Sierra and Bert Bates for the same.

Please provide your views

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!