my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic" Watch "generic" New topic
Author

generic

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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?


SCJP-1.5<br />SCWCD-1.4
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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

Joined: Feb 28, 2007
Posts: 1707


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

Joined: Jun 18, 2007
Posts: 383
Thanks Chandra.
I got it.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

If you want your code to give you a warning instead of a compiler error try this modification:



SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
 
GeeCON Prague 2014
 
subject: generic