Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes why we need to declare the collection classes polymorphically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why we need to declare the collection classes polymorphically" Watch "why we need to declare the collection classes polymorphically" New topic
Author

why we need to declare the collection classes polymorphically

Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
Hi All,


What is the benefit of declaring the collection classes polymorphically




thanks and regards,
Mahes
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Quoting this page :
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.


[My Blog]
All roads lead to JavaRanch
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Christophe Verré wrote:Quoting this page : When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.


As far as I understand the question is about the OO design principle "Program to interface not implementation", it is not about generics. There are many advantages, biggest one being loose coupling and flexible design. It allows for the implementation to be changed with minimal code changes.

This is one of the core OO design principles. To really appreciate the benefits, it would be a good idea to read a book on design patterns. Head first series has a good one on this topic that makes the design principles and design patterns easy to understand.

 
wood burning stoves
 
subject: why we need to declare the collection classes polymorphically
 
Similar Threads
html:options tag and String array
Generics problem
question about super
Chapter 7 Question14 : wrong answer
one problem in CASTOR parsing...