Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interfaces And Casting Array Components

Status
Not open for further replies.

huttemannw

Programmer
Feb 6, 2003
12
0
0
US
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();
}
}
}
}
 
Your robots-Array is of type Robot.
Robot has no method 'stop'.
So you have to cast it to something which has a stop-method.

Your Locomotion has a method 'stop ()'.
What if you add a method 'stop ()' to Sound?

The concept of type-safety forces us to make sure, that a methodcall will suceed, and be unambiguousely.

seeking a job as java-programmer in Berlin:
 
So, you're saying that the statement:

robots[index]

returns only the reference and not the object itself, which would require a cast (because the reference is of type Robot which doesn't implement Locomotion)?

And, that the statement:

Locomotion loco = robotB

returns the object itself, negating the need to use a cast?

Thanks again for your reply. I'm beginning to see the light.
 
And, that the statement:

Locomotion loco = robotB

returns the object itself, negating the need to use a cast?

I would say these questions don't allow vague descriptions.
And the line lacks at least a semicolon and introduces something new: 'robotB'.
Is this a
Code:
RobotB robotB = new RobotB ();
// or 
Locomotion robotB = new RobotB ();
// or 
RobotB robotB = robots[1];
// or
Object robotB = robots[1];
// or
// ...

I guess I know what you mean (ex.1), and you're getting it, but I would express it in different words.
If you have a hierarchie, with an arrow meaning --> extends and this one ...> meaning implements:
D --> C --> B ...> A
you don't need a cast to use 'C' as 'B' or 'A' - because 'C' IS a B and an A and must be one.
But not every C must be a D - it only CAN be a 'D'.
With a cast, you tell the compiler 'Hey! I know, it's a 'D', gimme this 'C' as a 'D'.
If it isn't a 'D', you get an ClassCastException.

seeking a job as java-programmer in Berlin:
 
Now I understand. I really appreciate your help. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top