|
Re: Upcasting
|
Posted: May 12, 2005 6:12 AM
|
|
Upcasting spares some work.
Example: a JPanel contains some JLabels and some JTextfield and is set up with a null layout.
We want to move all elements to the right.
If the objects would be stored as JLabel and JTextfield, we would have to write 2 different loops: one handling the JLabels, the other handling the JTextFields.
But since both extend the class Component and this class offers all needed methods, we cast them to Component, so we only need one operation with one loop.
You will never write like doSomethingWith((Component)myJFrame);
The upcasting is done by the compiler.
|
|