Menu DEVPPL Search
160.808
MEMBERS
15.941
TOPICS
63.227
POSTS
  562
FLASH GAMES
7.717
TUTORIALS
 

Login

E-mail:
Password:

Without casting, is there anyother way?

0

Loading

Without casting, is there anyother way?

Postby PaulSeldon » 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
 
Reputation: 0
Posts: 1
Joined: Sat May 19, 2007 6:34 pm
Highscores: 0
Arcade winning challenges: 0

Without casting, is there anyother way? - Sponsored results

Sponsored results

Login to get rid of ads

 

0

Loading

Postby Meongmania » 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
 
Reputation: 0
Posts: 16
Joined: Tue Dec 19, 2006 3:58 am
Highscores: 0
Arcade winning challenges: 0
^ Back to Top