It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why invalid syntax? 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 "Why invalid syntax?" Watch "Why invalid syntax?" New topic
Author

Why invalid syntax?

Hanna Barenthin
Greenhorn

Joined: Mar 13, 2007
Posts: 14
Hi, I do not understand why class GenTest<T super Number>{} is invalid syntax. Anyone who does? The question has arisen from the mock exam question below, question 9 in

http://www.javabeat.net/javabeat/scjp5/mocks/Generics-part3-10questions.php


Choose the valid ways to create an object for the following class.

class GenTest<T super Number> {

T num;

public T checkNumber(T n) {

return n;

}

}



Options :

a) Compilation fails.

b) GenTest<Number> gt = new GenTest<Number>();

c) GenTest<Integer> gt = new GenTest<Integer>();

d) GenTest<Object> gt = new GenTest<Object>();

e) None of the above.


Answer :

A is the correct answer.

Since <T super Number> is an invalid syntax. If super keyword is replaced by extends, then B and C will be the valid answers.

Many thanks
Hanna
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hanna Barenthin,

Interesting point.

Actual class:
class Box<T super Number> {
private T value;
public Box(T t) { value = t; }
...
}
//After Type Erasure:
class Box {
private Object value;
public Box(Object t) { value = t; }
...
}

Type Erasure can only replace occurances of T with Object.
why bec'ze its the super class to all the classes in java.

Here you can use Object methods, Which is why "T super Number" is meaningless to use.
Hope its clear for super.
-------------------------------------------------------------
// Lets see whas happenning for <T extends Number>

class Box<T extends Number> {
private T value;
public Box(T t) { value = t; }
...
}
Example (of use of upper bound on a type parameter in type erasure - after type
erasure):
class Box {
private Number value;
public Box(Number t) { value = t; }
...
}

So meaningful here to pass Box<Ineger>, Box<Float>. No problem.

Have a nice day.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Hanna,

If you have doubts (or)
If you wanna be perfect with Generics
AND NOT NEEDED FOR CERTIFICATIONS

Refer this link
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.pdf

Lets Rock
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Srini,


As I think, 1 will be the correct. So why isn't good for compiler to see the
class GenTest<T super Poodle>.
Will you please focus light on this?

Thanks and Regards,
cmbhatt


cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557

class Animal {
}
class Dog extends Animal{
}
class Poodle extends Dog {
}

class GenTest<T super Poodle> { //compiler error
}



GenTest<T super Poodle> : Means,It has accept all of these,

1)GenTest<Poodle>,
2)GenTest<Dog>
3)GenTest<Animal>
4)GenTest<Object>

Now Check again ... Chandra
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Nay Srini,

It is not, what I wanted to ask,

Why does following class give compiler error
class GenTest<T super Poodle> {
}

Thanks,
cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Becuase of Type erasure,
T in GenTest class will have to be replaced with Object.
No other option.
So There is no use of storing everything into Object because
1)we can only use Object methods (or)
2)type cast to lot many super classes to avail their methods.

Thats why ClassName<T super xxx> supressed.
No use of that. Got it ?

[ April 01, 2007: Message edited by: Srinivasan thoyyeti ]
[ April 01, 2007: Message edited by: Srinivasan thoyyeti ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
That's Good Srini,

Still "BUT" is there

class GenTest<T extends Poodle> {
}

What will be it's type erasure? Is it "class GenTest<Poodle> {}" ?
I think it should be!

Anyways,

Thanks,
cmbhatt
[ April 26, 2007: Message edited by: Chandra Bhatt ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

I think your compiler got some virus.

Please look into this these faqs for more information.
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.pdf

They are really cool stuff.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Srini,

Sorry for waking the Zombies!!!

But as per my subsequent replies to Swarna Dasa, he introduced
this link to me, which was sleeping somewhere on the server disk.


Originally posted by Srinivasan,
GenTest<T super Poodle> : Means,It has accept all of these, //Line 1

1)GenTest<Poodle>,
2)GenTest<Dog>
3)GenTest<Animal>
4)GenTest<Object>


Assure that it is not allowed in the generic class definition or
declaration of the parameterized type in the method.
As:




Thanks,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why invalid syntax?
 
Similar Threads
Generics in class declaration
Query on Generics
Arrays of generics
Generic classes
Generics Basic doubt