• 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

Doubt on interface

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AnInterface ai = new AnInterfaceImpl();

where AnInterface is an interface and AnInterfaceImpl is the class which implements AnInterface
Does it make any sense do this?What is the purpose of a statement like this
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does make sense because the ISA relation holds.
The purpose is same as that of a ISA relation. The interface type reference can hold objects of any of its implementers.
And this is used in many cases like

This is used to facilitate runtime polymorphism.


Hope this helps
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This gives you flexibility to your code. In future if you want to create an instance of another class named AnInterfaceImpl1(), you can change it easily by changing your code to


Design perspective, this type writing style is known as Program to an Interface .
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good example are method signatures in an API. If you've defined a method

then it's very hard to change the type of the parameter or the return type, if later you discover that some other kind of List or Map would now be more appropriate.

If you had defined it like

then you can accept and return any kind of List/Map that is convenient (maybe a LinkedList instead of an ArrayList).
[ August 24, 2008: Message edited by: Ulf Dittmer ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general idea is, declare something as broad as possible.

For example, go for Number instead of Integer if you don't care what numbers you need.
Also, go for Collection as much if possible. Only use List or Set if you really need the properties of either one (like the indexing or the unique elements).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic