File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes type Cast btn LIst and Vector, ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "type Cast btn LIst and Vector, ArrayList" Watch "type Cast btn LIst and Vector, ArrayList" New topic
Author

type Cast btn LIst and Vector, ArrayList

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Friends
i want to know the advantage of


List li = new Vector();
Arraylist al = (ArrayList)li;

why it's giving me an error ???

Please do clarify if somebody has idea ...


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Both Vector and ArrayList are implementing the List interface. But you can't assign a class of type Vector to ArrayList. its basic OO rule. Lets take in this way, CLASS B AND CLASS C IMPLEMENTS INTERFACE A.

Then

A Object = new B(); ok

A Object1 =new C(); ok

but B object=new C(); Not OK

you can assign all its subclasses to super interface but not sub class to subclass.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

then what is the
1)List li = new Vector();
2) Vector vec = new Vector();
what adv am i getting by declaring in style 1. rather than style 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
When you use style 1), you are free to change it to use an ArrayList, LinkedList or some other List implementation, and can be sure that it will still compile. That is, it decouples your code from a specific List implementation and thereby increases its flexibility.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

HI Ilja ,

Thanks a lot for your clarifiaction , pLease let me know how can change the style 1 ) to linkedList implementation , i tried for the same and end up with some exception like ClassCAstException .
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Think about polymorphism and usages , then It will give why that 1) is good.

Let me know still you have doubt ...

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Srinivas
Originally posted by Srinivas Kalvala:
Hello,

Think about polymorphism and usages , then It will give why that 1) is good.

Let me know still you have doubt ...


hi
yep still i do have doubt

pLease let me know how can change the style 1 ) to linkedList implementation from ArrayList as Ilja Preuss said
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

hi
yep still i do have doubt

pLease let me know how can change the style 1 ) to linkedList implementation from ArrayList as Ilja Preuss said


I think you misinterpreted what Ilja said... he was referring to decoupling, which allows you to write code that doesn't depend on the type of the object. He wasn't saying that it was possible to cast one type to another.

For example...



You have a code that takes a list and only works with the list interface. It doesn't cast that list back to a vector or an arraylist -- it merely works with the list. Then...



You can use that same method with an ArrayList, or a LinkedList, and it should work. The method only cares about the List contract, it doesn't care about the actual implementation.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Thanks Henry Wong for your clarification , i think thats what srinivas has menioned as polymorphic behaviour....
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Gowtham Ganamukala:
Thanks Henry Wong for your clarification , i think thats what srinivas has menioned as polymorphic behaviour....


Yes. The point is that you should write the code so that you *don't need* to cast to something more specific than List. Then and only then can you later change which actual implementation you use.
Prathap Adusumilli
Greenhorn

Joined: Aug 23, 2006
Posts: 4
A good rule of programming is to

"Program to an Interface not to an Implementation".

So List li = new Vector();
is cleaner and easily maintainable than Vector vec = new Vector();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type Cast btn LIst and Vector, ArrayList
 
Similar Threads
which is faster in List Classes
ArrayList vs. Vector
advantage Doublylink list over linklist
URLyBird interface?
vectors-arrays