title: Passing an object of unknown type as parameter in a Java method
body: <p>I'm creating a couple of classes to simplify processes like creating GUIs, applying accessibility settings, drawing graphics, etc. and I've come a long way; but I've had an idea that would be great it if could work, and I'm wondering if it's possible.</p>
  
  <p>Right now, I have a "JFrameBuilder" class, with methods like:</p>
  
  <pre><code>/**
   * The "addButton" method adds a button to a JFrame
   * @param app The parent window (a JFrame)
   * @param text The button text
   * @param x The x-coordinate where the button should appear
   * @param y The y-coordinate where the button should appear
   * @param width The width of the button
   * @param height The height of the button
   * @return A JButton object (it returns an object so programs can manipulate it later)
   */
  public JButton addButton(JFrame app, String text, int x, int y, int width, int height) {
          try {
              JButton button = new JButton();
              button.setText(text);
              button.setBounds(x, y, width, height);
              app.add(button);
              System.out.println("\"" + text + "\" button created.");
              return button;
          }catch(Exception e) {
              System.out.println("Something went wrong when creating the \"" + text + "\" button.");
              System.out.println(e.getStackTrace());
              return null;
          }
      }
  </code></pre>
  
  <p>So basically all it's really doing is setting multiple properties in one method (eliminating the need to write 10-15 lines of code per object).  These work great, but I'd like to abstract it further, to where the methods can work with JApplets, JPanels and other containers as well as JFrames.  That way I could generalize the class from JFrameBuilder to a more straightforward "AppBuilder", "GUIBuilder" etc. and be able to use it in other projects.</p>
  
  <p>But to do this, I would need to pass an object of an unknown type as a parameter in the methods.  So I tried replacing "JFrame" with "Object", then using a String to identify its type, and tried to make changes based on that.  However, I ran into some trouble using methods like "add"; I'm thinking it's because Object doesn't have an add method (even though a JPanel was the object type and JPanel does).</p>
  
  <p>To clarify a bit, basically what I need it to do is figure out the container where the button/label/whatever should be added (the "parent?").  And so far, the only way I know to do that is by passing it as a parameter in a method; if there's another way, I'd like to learn it, but if not, then ideally passing an unknown-type variable seems to be the best way.  That way there would be something inside the method to identify the type and base its next actions on that (like for example, if can display other objects, add to it, and if not, catch the exception).</p>
  
  <p>So I checked out similar questions on this site (which was actually where I got the Object idea from) and also Googled around a bit, but with no luck.  So as always, I would definitely appreciate any suggestions or other ideas.</p>
  
  <p>Thanks.  : )</p>