The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


RTTI and class hierarchy

Posted by Simon Bonner on June 19, 2001 at 8:18 AM


I'm a Java newbie currently working through Bruce Eckel's book 'Thinking in Java'. I just got to the chapter on RTTI and I have a question that the book doesn't seem to answer. If you have a base class from which several other classes are derived, e.g.

class Shape {}
class Square extends Shape {}
class Circle extends Shape {}
class Triangle extends Shape {}

Is there any way of starting with the 'Shape' class, and determining what classes are derived from 'Shape'. I.e. can I do something like

Class subclassesofShape[] = Shape.class.getSubClasses();

...and receive an array containing the classes 'Square', 'Circle' and 'Triangle'?

Simon Bonner


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us