• 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

Object : Cosmic Super Class

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application in which I am using a JTable which displays lots of custom objects as row data.
Since I wanted to override only the text representation of these objects, I set a default renderer for the Object class, hoping that it would work just fine.
However, I observed it was not working correctly for the classes like Long and Integer. When I peeked into the source code, I realised that the Number class does NOT extend the Object class.

Anyone know why this is? I was under the impression that Object is the cosmic super class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maneesh Godbole:
When I peeked into the source code, I realised that the Number class does NOT extend the Object class.



It does, implicitly.

That is, when the compiler doesn't find an extends clause in the class declaration, it automatically adds an "extends java.lang.Object" for you.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:


It does, implicitly.

That is, when the compiler doesn't find an extends clause in the class declaration, it automatically adds an "extends java.lang.Object" for you.



But, my renderer did not work for Long. Any idea why? I had set it to Object.class
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a "proof", execute

System.out.println(Number.class.getSuperclass());
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maneesh Godbole:

But, my renderer did not work for Long. Any idea why? I had set it to Object.class



JTable automatically registers renderers for some classes (see the source code of JTable.createDefaultRenderers). And it uses the most specific renderer it can find for a column. So if you want those classes to be rendered by the renderer registered for Object, you will have to delete the more specific ones by setting them to null. For example:

myTable.setDefaultRenderer(Long.class, null);
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic