| Author |
mock test for generics & collections
|
dolly shah
Ranch Hand
Joined: Jun 18, 2007
Posts: 383
|
|
Can anybody give me a link which has generics & collections examples? I need so. Thanks in advance.
|
SCJP-1.5<br />SCWCD-1.4
|
 |
Padma Asrani
Ranch Hand
Joined: Mar 22, 2007
Posts: 111
|
|
Hi Dolly, This has some questions. http://www.javabeat.net/javabeat/scjp5/mocks/index.php Regards Padma
|
 |
dolly shah
Ranch Hand
Joined: Jun 18, 2007
Posts: 383
|
|
import java.util.*; 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 b = a; ArrayList<Bus> c = (ArrayList<Bus> b; c.add(new Bus()); for (Object obj : b) System.out.println(obj); } } Here b=new ArrayList<Car>(), when we add new bus then it is adding in a car arraylist. So it can not compile. I want to make sure that I am right or not?
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
Your code compiles with a warning in line It also runs fine. [edit] I hate smilies. BTW, why is it that smiley replacement is also done inside code blocks? [ June 26, 2007: Message edited by: Sergio Tridente ]
|
SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
|
 |
dolly shah
Ranch Hand
Joined: Jun 18, 2007
Posts: 383
|
|
class Box<T> { private T theObject; public Box(T arg) { theObject = arg; } public Box() { } } class Test { public static void main(String[] args) { Box<String> b1 = new Box<String>("Java"); //line 1 Object<String> b2 = new Box<String>("Struts"); //line 2 Box<String> b3 = new Box("EJB"); //line 3 Box b4 = new Box<String>("Ajax"); //line 4 Object b5 = new Box<Object>(); //line 5 } } what will be the effect of the program? a)compile time error at line 5 only b)compile time error at line 2 only c)compile time error at line 2 and line 5 d)compile time error at line 3 and line 4 e)Run time error Answer : b Explanation: Object class doesnot take any parameters Why is it so? I am not getting. Can anyone explain?
|
 |
CrystalCracker
Greenhorn
Joined: Jun 07, 2007
Posts: 3
|
|
|
Shouldn't it give a runtime exception while we are trying to cast b ( which is a) to c?
|
 |
Padma Asrani
Ranch Hand
Joined: Mar 22, 2007
Posts: 111
|
|
hi
class Box<T> { private T theObject; public Box(T arg) { theObject = arg; } public Box() { } } class Test { public static void main(String[] args) { Box<String> b1 = new Box<String>("Java"); //line 1 Object<String> b2 = new Box<String>("Struts"); //line 2 Box<String> b3 = new Box("EJB"); //line 3 Box b4 = new Box<String>("Ajax"); //line 4 Object b5 = new Box<Object>(); //line 5 } }
No idea. I rather thought there shouldn't be any errors because if we can say List <Integer> a = new ArrayList<Integer>(); And List is a super class of ArrayList, so we can definitely assign object of ArrayList to the reference to super class type(List) in this case. So if we follow the same reasoning then the number#2 should be allowed but indeed it is not. Any thoughts. Note: There is one suggestion. Instead of continuing the same thread you can start a new thread because the subject is misleading. Regards Padma
|
 |
Sergio Tridente
Ranch Hand
Joined: Mar 22, 2007
Posts: 329
|
|
Originally posted by dolly shah: Object<String> b2 = new Box<String>("Struts"); //line 2
Object class is not generic, i.e. it does not take a type argument.
|
 |
Padma Asrani
Ranch Hand
Joined: Mar 22, 2007
Posts: 111
|
|
Hi Thanks!!! we missed the trivial point there. Regards Padma
|
 |
 |
|
|
subject: mock test for generics & collections
|
|
|