Win a copy of Testing JavaScript Applications 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

benefits of using base class/interface reference to refer sub class objects.

 
Ranch Hand
Posts: 41
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the main benefits of using base class reference to refer to sub class objects as doing List list=new Arraylist(); same for other custom classes.

one thing that i know is the re-usability of reference, is there any other advantage.
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I did not understand what you meant by "re-usability of reference".
The whole concept of polymorphism in java relies on java's ability to use interface/base class reference to reference to sub class objects.
Also, programming to an interface rather than to the actual implementation is always seen as a best practice. This would allow the developer to replace one implementation with other/better implementation easily when required.
Example is you can simply change "List list=new Arraylist();" to "List list=new Linkedlist();" and leave the rest of the application untouched.
 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by re-usability of reference i mean the same you explained "List list=new Arraylist();" to "List list=new Linkedlist();" reusing the same reference list to refer to Arraylist and then Linkedlist.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a 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
    Bookmark Topic Watch Topic
  • New Topic