Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Regarding creating object in java

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

Could you please tell me the difference between creating the object, Which i have given below

1 Set s=new HashSet();
2 HashSet s= new HashSet();

Thanks,
Santhosh Kumar VK
 
author
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh kumar vk wrote:
Could you please tell me the difference between creating the object, Which i have given below

1 Set s=new HashSet();
2 HashSet s= new HashSet();



Both code examples created a HashSet instance using the no-args constructor to initialize it. There should be no differences between those two created objects.

As for the code differences, the first one declares a Set reference and the second one declares a HashSet reference... that points to the created object.

Henry
 
Saloon Keeper
Posts: 24180
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to expand on Henry's explanation, java.util.Set is an interface, meaning that it defines a contract that a class that implements the interface must honor. HashSet is merely one class that implements the Set interface. TreeSets, EnumSets and LinkedHashSets are just a few of the other classes that implement Set (and, of course, you can design your own Set implementation classes).

The advantage of defining s as a "Set" instead of as a "HashSet" is that you're defining the behavior of s in terms of its contract instead of in terms of its implementation. You can assign a HashSet object to a Set variable with impunity, but assigning some other Set object (such as TreeSet) would fail to compile or throw a ClassCastException (depending on how dynamic the code was).

If you program in terms of contracts instead of implementations, you have more flexibility. You can come back and swap out (or subclass) the object types to provide better functionality or performance and not have to do as much downstream code modification.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic