5 Replies Latest reply: Dec 28, 2008 4:45 PM by 796440 RSS

    Class inheritance or Accessing methods

    807589
      Hi,
      I have two classes in the same file. One of the classes has an inner class. Here is an example and my problem.
      The following code is in one file, for instance A.java
      
      public class A{
           public method1(){...}
      } // end of class A
      
      class B{
           inner-class AB{System.out.println("How do I access method1 from here?"); }
      }// End of B
      So, how do I access method 1 from from inner-class AB?

      Appreciate any helpful input on this. Thanks.
        • 1. Re: Class inheritance or Accessing methods
          807589
          You call an instance method of a class by getting an instance (being passed a reference, or obtaining one with "new") and ... calling the method.

          That AB is an inner class of B doesn't change this. Either pass the method (or constructor) that wants to access an instance method of A, a reference to an instance of A or have it create one with "new".

          Edited by: pbrockway2 on Dec 29, 2008 11:24 AM

          The question would be a little clearer if you posted compilable code. It's not clear whether you want to "access method1" from a method/constructor or to initialise a variable some other way. Also "static" makes a difference.

          More clarity would be added if you said what you are trying to do (!= how you are trying to do it).
          • 2. Re: Class inheritance or Accessing methods
            807589
            Sorry, but i wanted to add more info, just now and I can't. Class A extends JApplet and Class B extends JPanel, so I can't seem to get around that with the "new" keyword.

            I hate this design of the forum, cause it doesn't allow us to either delete our post or edit it after a while. I can't believe how the designers of a programming language would make such a horrible forum. This sucks.
            • 3. Re: Class inheritance or Accessing methods
              807589
              No biggie, just repost your code. but what method of a JApplet would a JPanel need to call? Most of my "JApplet" derived classes simply create the applet and add a JPanel to the contentPane, that's it. i.e.,
              import javax.swing.JApplet;
              
              /**
               * create a JApplet and add animation panel to the 
               * applet's contentPane 
               * @author Pete
               */
              public class MyA_Applet extends JApplet
              {
                public void init()
                {
                  try
                  { // create my class and display in a thread-safe way
                    javax.swing.SwingUtilities.invokeAndWait(new Runnable()
                    {
                      public void run()
                      {
                        createGUI();
                      }
                    });
                  }
                  catch (Exception e)
                  {
                    System.err.println("createGUI didn't successfully complete");
                  }
                }
              
                private void createGUI()
                {
                  // create animation panel and add the mainPanel to the applet's content pane
                  getContentPane().add(new MyAnimationPanel().getComponent());
                }
              }
              Edited by: Encephalopathic on Dec 28, 2008 2:34 PM
              • 4. Re: Class inheritance or Accessing methods
                807589
                but by passing references, you could have a detail class object call methods from a "main" class object. For instance:
                public class MainClass
                {
                  private DetailClass detail = new DetailClass(this);
                  public void methodInMain(String sourceString)
                  {
                    System.out.println("Method in Main.  Source: " + sourceString);
                  }
                  
                  public void callDetailsMethod()
                  {
                    detail.detailMethod();
                  }
                  
                  public static void main(String[] args)
                  {
                    MainClass mainObject = new MainClass();
                    mainObject.callDetailsMethod();
                  }
                }
                
                class DetailClass
                {
                  private MainClass mainObject;
                  
                  public DetailClass(MainClass mainObject)
                  {
                    this.mainObject = mainObject;
                  }
                  
                  public void detailMethod()
                  {
                    mainObject.methodInMain("From detailMethod");
                  }
                }
                • 5. Re: Class inheritance or Accessing methods
                  796440
                  ChangBroot wrote:
                  Hi,
                  I have two classes in the same file.
                  That is irrelevant here.
                  So, how do I access method 1 from from inner-class AB?
                  Exactly the same way you'd access it if class A were in different source file.