This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics - Bounded Type Parameters - Query  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I slightly changed the code from Link

class Box<T> {
private T object;

public void add(T object) {
this.object = object;
}

public T get() {
return object;
}

public <U extends Integer> void inspect(U u) {
System.out.println(object.getClass().getName());
System.out.println(u);
System.out.println(u.getClass().getName());
}
}

public class BoxDemo {
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();

integerBox.add(new Integer(10));
Integer someInteger = integerBox.get();
System.out.println(someInteger);
integerBox.inspect(new Short(8));
}
}

It is giving my Compile error at integerBox.inspect(new Short(8));

Cannot find symbol Constructor Short(int). What could be the problem here?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short is not a subclass of Integer but a sub class of Number.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer is a final class and cannot be subclassed.
 
Sheriff
Posts: 9611
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marthi! Welcome to javaranch.

Well there are two errors in your code. One of them has been pointed out by victor. The other one is that the Short class constructor takes a short value. You are passing 8 to it which is an int. And as you might know, implicit downcasting is not done in method calls. So you should change the code as

new Short((short)8)
 
marthi reddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Ankit and Victor for the information. This solved the error that I was encountering.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!