• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

polymorphical issue on collection

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

Why we are initializing an ArrayList like this

Instead of

What is the difference between those statements
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maheswaran Devaraj wrote:

What is the difference between those statements


The difference is Use of Generics.
When you declare the the type of a list object as a String, you need not do the explicit type casting while retrieving the value from list, moreover, It guarantees that, no other than String object allowed to insert into ArrayList.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please try to understand that ArrayList inherits from List. Most of the books will go with using is nearly same as writing . Now is actually a non-benefit code.
You need to understand the reason for using Generics. Generics are used primarily to ensure Type Safety. Notice when you use the first and second statements you mention the data type(i.e. String) the list will be using hence preventing the list being accidentally used for some other types making it types safe...However the third definition is similar to writing in which you can pass any types which is not at all type safe. I wish I have explained you. I am still learning JAVA correct me if I am wrong..I will appreciate it.
 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having another one query regards this

please give me if any differences for these two initializations:




Thanks in advance,
Mahes
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List is a interface and ArrayList is a class which implements the List interface.
The inteface defines few methods.And arrayList implements those interface methods besideS few of its own methods.
So if you declare a variable using the List reference then you can call only the interface methods.
But if you decalre the variable using ArrayList then you can call those few extra methods.
But using the interface refernce has its own advantages .(Hope you know that.)
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just like,
Car car = new Ferrari();
To clear the polymorphism concept, read this, its excellent article !

Just like, simple
Car car = new Car();

Just don't get confused because of the use Of generics, it doesn't breaks any rule of Object instantiation!!
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget good ol' type safety, one of the main reasons for generics:

(psuedo code)




wheras;




So whilst this may not be an issue if you don't mind what your list holds, but what happens if you then do this?



all fine till you reach new Dog(), how does that use the append method(), bang blows up!

 
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