• 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

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

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic