Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

type Cast btn LIst and Vector, ArrayList

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong for your clarification , i think thats what srinivas has menioned as polymorphic behaviour....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic