• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generic

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra.
I got it.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want your code to give you a warning instead of a compiler error try this modification:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic