Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:36:49+00:00 2026-05-13T14:36:49+00:00

How can I get the the superclass of a Class instance in Java ME.

  • 0

How can I get the the superclass of a Class instance in Java ME. That is, fake the Class.getSuperclass() functionality with the limited functionality available in CLDC 1.1?

What I want to do is to let the abstract super class do something like this:

public Styler getStylerForViewClass(Class clazz) {
   Styler s = stylers.get(clazz);
   if (s == null) {
     for (Class c = clazz; s == null; c = c.getSuperclass()) {
       if (c == Object.class) {
         throw new IllegalArgumentException("No Styler for " + clazz.getName());
       }
       s = createStylerForViewClass(c);
     }
     stylers.put(clazz, s);
   }
   return s;
}
public Styler createStylerForViewClass(Clazz clazz) {
  if (clazz == View.class) {
    return new DefaultStyler();
  } else {
    return null;
  }
}

Sub classes could then add specializations like this:

 public Styler createStylerForViewClass(Class clazz) {
   if (clazz == SpecialView.class) {
     return new SpecialStyler();
   } else {
     return super.createSylerForViewClass(clazz);
   }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:36:49+00:00Added an answer on May 13, 2026 at 2:36 pm

    As you have already discovered, MIDP does not provide a method for getting the superclass of a class, nor for enumerating all classes in the application.

    So all you can do is keep track of the class hierarchy yourself.

    Having a common superclass makes it slightly easier, because you can have the new object add its own class to a global class collection (if not already present) in the superclass constructor:

    abstract class View {
        protected View() {
            classHierarchy.add(this.getClass());
        }
    }
    

    but unfortunately this will not work for abstract classes, because no instances are ever created.

    Keeping track of superclass/subclass relations for a known subset of classes is easy enough. e.g.:

    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Vector;
    
    public class ClassHierarchy {
     public ClassHierarchy() {
      childToParentMap = new Hashtable();
      parentToChildMap = new Hashtable();
      parentToChildMap.put(Object.class, new Vector());
     }
    
     public boolean addClass(Class toAdd) {
      if (toAdd.isInterface()) return false;
      if (toAdd.equals(Object.class)) return false;
      if (childToParentMap.get(toAdd) != null) return false;
    
      addClassBelow(toAdd, Object.class, new Vector());
      return true;
     }
    
     public Class getParent(Class subclass) {
      return (Class) childToParentMap.get(subclass);
     }
    
     private void addClassBelow(Class toAdd, Class parent, Vector initialChildren) {
      Vector children = (Vector) parentToChildMap.get(parent);
      Class reparented;
      do {
       reparented = null;
       for (Enumeration childEnum = children.elements();
            childEnum.hasMoreElements();
            ) {
        Class child = (Class) childEnum.nextElement();
        if (child.isAssignableFrom(toAdd)) {
         addClassBelow(toAdd, child, initialChildren);
         return;
        } else if (toAdd.isAssignableFrom(child)) {
         children.removeElement(child);
         initialChildren.addElement(child);
         childToParentMap.put(child, toAdd);
         // Guard against concurrent modification
         reparented = child;
         break;
        }
       }
      } while (reparented != null);
    
      children.addElement(toAdd);
      childToParentMap.put(toAdd, parent);
      parentToChildMap.put(toAdd, initialChildren);
     }
    
    
     private Hashtable childToParentMap;
    
     private Hashtable parentToChildMap;
    }
    

    But this can “miss” intermediate classes that are added later, e.g. if you have these classes:

    Object >= View >= A >= B >= C
    

    and add A and C to the tree and asked it for the superclass of C it would give you A, and if you later added B it would replace A as the superclass of C, but not until the wrong styler had been returned for some instances of C.

    So I think you will have to add the restriction that ancestor classes (that have stylers defined for them) must be added to the tree first. Possibly from the static initializer block of the class that overrides createStylerForViewClass, or the static initializer of the view class itself.

    I did think of one other evil hack, but I can’t really recommend it:

    • In the View constructor, create a new Exception, but don’t throw it.
    • Temporarily swap System.err for your own writer that writes to a ByteArrayOutputStream
    • Call printStackTrace() on the exception
    • Restore System.err to its original value
    • Parse the stack trace from the ByteArrayOutputStream. The names of the intermediate classes’ constructors will be in the stack trace. Now you can look them up using Class.forName() and add them to the tree.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 283k
  • Answers 283k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Once you start using a SurfaceView, you are moving yourself… May 13, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer Our goal with using the "no-cache" directive was to prevent… May 13, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer It in theory it can. Yet at the same time… May 13, 2026 at 4:19 pm

Related Questions

Is there a way in Python, to have more than one constructor or more
I'm trying to create a C++ class, with a templated superclass. The idea being,
I have a method with this signature: protected final Map<String, Object> buildOutputMappings( AbstractDataObject ado,
I've defined a method in a class: public void setCollection(Collection<MyClass>); and in another class
I'm learning PowerShell 2.0 on Windows 7. My task is simple: I want to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.