• 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

Having Interface on the left and Class on the right side

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

I always wondered why people do this, Set s = new HashSet() or Set s = new TreeSet() etc...Since I am readin and workin on collections now, I think it's because maybe later on if we want some additional methods from TreeSet which are not there in the HashSet, all we need to do is change new HashSet() to TreeSet() right? The code will not break and we need not do more work where as if we do, HashSet s = new HashSet() we need to change it on the left and on the right hand side too. Am I correct?

Thanks.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arjun,

To my knowledge you are right. Let us see what others say.

Ananth Chellathurai
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Its all about polymorphism. Any variable of type superclass or interface which is implemented by a class (in this case interface Set implemented by TreeSet or HashSet) can refer to any object which implements Set i.e., Set a=new TreeSet(); "a" is a variable of type Set and refering to object of type TreeSet. Here variable "a" is set to be polymorphic, i.e the ability to refer to more than one subclass or class which implements Set interface. One of the feature of polymorphism is to provide the ristricted access to subclass objects. Here using variable "a" you can access only the methods which are declared in the interface Set. In case of class, you can access only the overriden methods.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other use would be to use a single Reference to point to multiple objects.
For Example

Set s = new HashSet();

Now after calling some functions, you know that the work of HasSet Object is done, and you need TreeSet Object.
What you can simply do is

s = new TreeSet();

You dont need to create another reference especially for TreeSet Object. And the number of references used in your code will be reduced.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manju, please check your private messages. You can see them by clicking My Private Messages.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please answer in forum rather than sending privaet messages for such good questions

thanks javaranchers
 
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
mukki, jasper is not giving solutions in private messages. it is some administrative concern...
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying guys.
 
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 to throw it in here.... this is also an example of what you might see called "coding to an interface". For instance, Hashtable, HashMap, TreeMap all implement the Map interface. The Map interface specifies the basic methods required to use a collection that implements map. That said, if you write your code using the methods specified by the interface then, at "run time", that reference variable can hold any collection that implements Map. So any of the above three collection types could be "plugged into" that reference and it would work.
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic