JOIN DEVPPL

It looks like you are new here. If you want to get involved, click on Join below!



Without casting, is there anyother way?


P

keyboard_arrow_up
0
keyboard_arrow_down


First Post Mark as Spam paulSeldon
Posts: 1 - Registered: 2007-05-19 12:34:09

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


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();
}
}




M

keyboard_arrow_up
0
keyboard_arrow_down


Reply #1 Mark as Spam meongmania
Posts: 16 - Registered: 2006-12-18 20:58:58

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.






JOIN DEVPPL

It looks like you are new here. If you want to get involved, click on Join below!




MENU
Advertising