• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Difference between Interface reference and class reference

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

What is the difference between creating an instance using Class reference and Interface reference.

e.dg.

List lst = new ArrayList();

ArrayList alst = new ArrayList();


Which is better?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface provides the base for the concrete classes. It defines the contract which must be full filled by the implementing classes. So it can be use full in case where you need polymorphism. for example if you have some classes and interface like:



Here if you want to do some generic work which is common for both the doctors (doCheckUp) you should use the interface like


In this case no matter which object you are passing it will call the doCheckUp method of corresponding object.
But your concrete class can have some extra methods and if you want to use those methods then you have to call the methods using corresponding concrete class objects.

So If you want to use the generic common methods, you should use the interface instance.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example in case of ArrayList and List you can not user ensureCapacity method using List interface reference because this method does not come under List contract. It is defined in ArrayList itself.
 
Shailesh Nair
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was pretty suffiicent.

Thanks bro
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one thing to add to the "contract" notion..... when you refer to an object by interface all that you can reference are the methods/properties defined by the interface... nothing else. So, in effect, you are locking callers out of anything except the methods/properties of the interface. That is the part that limits the "coupling". The code that references the object knows the other object only by the interface... so any object that implements the interface can be "plugged in" without a problem.
 
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Ruth wrote:The code that references the object knows the other object only by the interface... so any object that implements the interface can be "plugged in" without a problem.



Sorry for waking up the zombies, but one question. If I used


and still used only the methods of List that were overridden by ArrayList(like get, add and not ensureCapacity), there ain't any coupling trouble, right? I mean if I blindly replaced ArrayList usage with Vector, there still won't be any problem. So what exactly does a reference to an interface offer you? Or is it just a programming convention?
 
We don't have time for this. We've gotta save the moon! Or check this out:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic