• 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

Generics - Bounded Type Parameters - Query

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer is a final class and cannot be subclassed.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Ankit and Victor for the information. This solved the error that I was encountering.
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic