• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 9643
42
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!