File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic

 
dolly shah
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

public class Vehicle {}

class Car extends Vehicle {}

class Bus extends Vehicle {}

class TestSamp {

public static void main(String [] args) {




ArrayList<Car> a = new ArrayList<Car>();

a.add(new Car());

ArrayList <Car> b = a;

ArrayList<Bus> c = (ArrayList<Bus> b;

c.add(new Bus());

for (Object obj : b)

System.out.println(obj);

Options:

1.compiler error


2.compiles with warning and gives some output


3.compiles without warning and gives some output


4.compiles and run with no output


answer

3) ArrayList b = a; This assignment assigns a typesafe arraylist in to a non-typesafe arraylist, So this assignment causes warning during compilation.

===============================================================================
When I wrote this program, I got compile error because "Incompatible cast(ArrayList<Car> to (ArrayList<Bus> ". Even the answer which they have given is also conflict. Can anybody help me?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dolly,

Problem comes because of incompatible casting. Polymorphism applies to base
type and not to the parameterized type.

List alist = new ArrayList(); //OK
Here List and ArrayList are base types, so it is ok.

List<Vehicle> blist = new ArrayList<Car>(); //NO
Here error comes because you violated the rule. Simply both the sides
the parameterized types must be same.

Note: Type unsafe ref variable can refer to object with any
parameterized type;
Like:

ArrayList alist = new ArrayList<Car>(); //OK, no warning though

ArrayList<Car> blist = new ArrayList(); // OK with warning



Thanks,
[ June 29, 2007: Message edited by: Chandra Bhatt ]
 
dolly shah
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Changra,
Thanks for explanation. I got your point.
But my question was, I got compiler error, and they have given answer is little conflict. As they have given answer 3, but that conflicts with their explanation.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ref variable b refers to ArrayList object parameterized with Car.
Now you are trying to cast that object to a ArrayList ref variable that
is parameterized with Bus. Against the rule of generics. This sort of
casting is not possible using generics.


Thanks,
 
dolly shah
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra.
I got it.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want your code to give you a warning instead of a compiler error try this modification:

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic