Without casting, is there anyother way?

TOPIC CREATED: 2007-05-19


p

keyboard_arrow_up
0
keyboard_arrow_down


First Post paulSeldon
Posted: 2007-05-19 17:41:49
((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();
}
}



1 Replies


m

keyboard_arrow_up
0
keyboard_arrow_down


Reply #1 meongmania
Posted: 2007-06-11 10:19:56
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