huttemannw
Programmer
I am currently in the process of teaching myself Java, and have a question concerning assigning array components to an interface reference.
The complete program listing is below, but here is the portion that I have a question about:
for (int index = 0; index < 7; index++)
{
if (robots[index] instanceof Locomotion)
{
Locomotion locomotion = (Locomotion)robots[index];
locomotion.stop();
}
if (robots[index] instanceof Sound)
{
Sound sound = (Sound)robots[index];
sound.beep();
}
}
Q: Why do I have to cast the array components to the interface type to make the assignments to the references locomotion and sound? If the array component references an object that implements the particular interface, it should not need casting. But it does, and it's driving me crazy.
I can assign a direct object reference to an interface reference with no problem, as illustrated:
// robotB is a direct reference to a RobotB object
// RobotB implements Locomotion
Locomotion loco = robotB;
I'm the type of person that has to understand why I'm doing something, and not just accept it. So, any help would be greatly appreciated.
Thanks.
Complete listing:
interface Locomotion
{
void forward();
void reverse();
void stop();
}
interface Sound
{
void beep();
}
abstract class Robot
{
}
class RobotA extends Robot
{
}
class RobotB extends Robot implements Locomotion
{
public void forward()
{
System.out.println("RobotB forward.");
}
public void reverse()
{
System.out.println("RobotB reverse.");
}
public void stop()
{
System.out.println("RobotB stop.");
}
}
class RobotC extends Robot implements Locomotion, Sound
{
public void forward()
{
System.out.println("RobotC forward.");
}
public void reverse()
{
System.out.println("RobotC reverse.");
}
public void stop()
{
System.out.println("RobotC stop.");
}
public void beep()
{
System.out.println("RobotC beep.");
}
}
class RobotA1 extends RobotA implements Sound
{
public void beep()
{
System.out.println("RobotA1 beep.");
}
}
class RobotB1 extends RobotB implements Sound
{
public void beep()
{
System.out.println("RobotB1 beep.");
}
}
class RobotB2 extends RobotB
{
}
class RobotC1 extends RobotC implements Sound
{
public void beep()
{
System.out.println("RobotC1 beep.");
}
}
class Chap_7_Cumul_1
{
public static void main(String args[])
{
Robot[] robots = new Robot[7];
robots[0] = new RobotA();
robots[1] = new RobotB();
robots[2] = new RobotC();
robots[3] = new RobotA1();
robots[4] = new RobotB1();
robots[5] = new RobotB2();
robots[6] = new RobotC1();
for (int index = 0; index < 7; index++)
{
if (robots[index] instanceof Locomotion)
{
Locomotion locomotion = (Locomotion)robots[index];
locomotion.stop();
}
if (robots[index] instanceof Sound)
{
Sound sound = (Sound)robots[index];
sound.beep();
}
}
}
}
The complete program listing is below, but here is the portion that I have a question about:
for (int index = 0; index < 7; index++)
{
if (robots[index] instanceof Locomotion)
{
Locomotion locomotion = (Locomotion)robots[index];
locomotion.stop();
}
if (robots[index] instanceof Sound)
{
Sound sound = (Sound)robots[index];
sound.beep();
}
}
Q: Why do I have to cast the array components to the interface type to make the assignments to the references locomotion and sound? If the array component references an object that implements the particular interface, it should not need casting. But it does, and it's driving me crazy.
I can assign a direct object reference to an interface reference with no problem, as illustrated:
// robotB is a direct reference to a RobotB object
// RobotB implements Locomotion
Locomotion loco = robotB;
I'm the type of person that has to understand why I'm doing something, and not just accept it. So, any help would be greatly appreciated.
Thanks.
Complete listing:
interface Locomotion
{
void forward();
void reverse();
void stop();
}
interface Sound
{
void beep();
}
abstract class Robot
{
}
class RobotA extends Robot
{
}
class RobotB extends Robot implements Locomotion
{
public void forward()
{
System.out.println("RobotB forward.");
}
public void reverse()
{
System.out.println("RobotB reverse.");
}
public void stop()
{
System.out.println("RobotB stop.");
}
}
class RobotC extends Robot implements Locomotion, Sound
{
public void forward()
{
System.out.println("RobotC forward.");
}
public void reverse()
{
System.out.println("RobotC reverse.");
}
public void stop()
{
System.out.println("RobotC stop.");
}
public void beep()
{
System.out.println("RobotC beep.");
}
}
class RobotA1 extends RobotA implements Sound
{
public void beep()
{
System.out.println("RobotA1 beep.");
}
}
class RobotB1 extends RobotB implements Sound
{
public void beep()
{
System.out.println("RobotB1 beep.");
}
}
class RobotB2 extends RobotB
{
}
class RobotC1 extends RobotC implements Sound
{
public void beep()
{
System.out.println("RobotC1 beep.");
}
}
class Chap_7_Cumul_1
{
public static void main(String args[])
{
Robot[] robots = new Robot[7];
robots[0] = new RobotA();
robots[1] = new RobotB();
robots[2] = new RobotC();
robots[3] = new RobotA1();
robots[4] = new RobotB1();
robots[5] = new RobotB2();
robots[6] = new RobotC1();
for (int index = 0; index < 7; index++)
{
if (robots[index] instanceof Locomotion)
{
Locomotion locomotion = (Locomotion)robots[index];
locomotion.stop();
}
if (robots[index] instanceof Sound)
{
Sound sound = (Sound)robots[index];
sound.beep();
}
}
}
}