It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming Java Forum

Without casting, is there anyother way?

Without casting, is there anyother way?

Postby paulSeldon on Sat May 19, 2007 6:41 pm

((ChildPanel)aPanel).aButton without casting like this,
is there any other way of doing it?


Code: Select all
package hiding.test;

public class MainTest {
   public static void main(String[] args) {
      
      MainWindow aMainWindow = new MainWindow() {
         public ChildPanel createPanel()
           {
              return new ChildPanel();
           }
         
         // --- 2nd ChildPanel
//         public SecondChildPanel createPanel()
//           {
//              return new SecondChildPanel();
//           }
         
         // --- 3rdChildPanel
//         public ThirdChildPanel createPanel()
//           {
//              return new ThirdChildPanel();
//           }         

      };
   }         
}


package hiding.test;

public class ParentPanel {

   public String aButton;

   public ParentPanel() {
      System.out.println("Hi, parent!");
      init();
   }
   
   public void init() {      
      aButton = new String("aButtonAtParent");
   }
}

package hiding.test;

public class ChildPanel extends ParentPanel {
   public String aButton;
   
   public ChildPanel() {
      System.out.println("Hi, Child!");
      init();
   }
   
   public void init() {      
      aButton = new String("aButtonAtChild");
   }
}

package hiding.test;

public class MainWindow {
   ParentPanel aPanel ;
   
   public MainWindow() {
      aPanel = createPanel();
      
      System.out.println("aPanel.aButton=" + aPanel.aButton);
      System.out.println("((ChildPanel)aPanel).aButton=" + ((ChildPanel)aPanel).aButton);

                                 //--- so many instance of aPanel...
      // aPanel.aButton
      // aPanel.aLabel etc ...
   }

   public ParentPanel createPanel() {
      return new ParentPanel();
   }
}
paulSeldon
 
Posts: 0
Joined: Sat May 19, 2007 6:34 pm

Postby meongmania on Mon Jun 11, 2007 11:19 am

I don't really understand what you really want, there is nothing wrong in casting an instance since the object itself IS an instance of the specified class. I think what you mean is polymorphism.
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 3:58 am


Return to Java Forum

Who is online

Users browsing this forum: No registered users and 0 guests