Bad Polymorphism

How come this dosnt work? I have a main class that calls two methods from two different classes

package general;

publicclass Test1{

private Test2 _test2 =new Test2();

private Test3 _test3 =new Test3();

public Test1(){

_test2.setName();

System.out.println(_test3.getNames()[0]);

}

publicstaticvoid main(String[] args){

new Test1();

}

}

the Test2 class is below

package general;

publicclass Test2{

private String[] names =new String[]{"John","Jack","Joe","Jim","Jeff","Jill","Jose"};

private Test3 _test3 =new Test3();

public Test2(){

}

publicvoid setName(){

_test3.setNames(names);

}

}

Theoretically when _test2.setNames() is called it should invoke _test3.setNames(names) and pass the names array into the Test3 class. Then the Test3 class is setup like this

package general;

publicclass Test3{

private String[] employeeNames =new String[7];

public Test3(){

}

publicvoid setNames(String[] _names){

employeeNames = _names;

}

public String[] getNames(){

return employeeNames;

}

}

when the names array is passed in, it sets it as the employeeNames String array, and the getNames method should return that String Array, and therefore when, in Test1, i print out _test3.getNames()[0] it should return John, but it is returning null. I know this code seems redundant but its just some pseudo code I threw together, I do have a reason for it :P

[3391 byte] By [SidewinderXa] at [2007-11-26 18:12:51]
# 1

There's a lot that can be criticized in your code, but one thing I notice is that

there is no polymorphism! So where's the bad polymorphism?

Anyway, I think your problem is that you have two distinct objects, named _test2 and _test3, and setting the names in _test2 is not going to affect _test3 -- that is why it's names are null.

DrLaszloJamfa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 2
You do realize that every Test2 object creates its own Test3 object?So your Test3 object in your Test1 class has nothing to do with theTest3 object 'owned' by your Test2 object in your Test1 class.kind regards,Jos
JosAHa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 3

Just a guess because I didn't go into your code in detail, but:

the _test3 in the Test1 class is a new Test3(), and the

_test3 in the Test2() class is another new Test3(), they are not the same instance of the object, so when you set the names for the _test3 in the Test2 class, the names of the _test3 in the Test1 class are not changed...

Hope that made sense.

abu5ea at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 4
Heh not quick enough. Maybe next time. You guys explained better anyway.
abu5ea at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 5

Your problem is very simple.

_test3 in Test 2 is a different object than _test3 in Test 1.

You gave both objects the same name in different classes

but that doesnt make them the same.

You can either pass a reference to you Test 3 object to Test 2 from

Test 1 or you can make the employee names in Test 3 static.

You and I go to Toys R Us together and by two teddy bears.

We name both our bears Teddy.

I rip the head of mine. Does yours still have a head?

TuringPesta at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 6

> You and I go to Toys R Us together and by two teddy bears.

> We name both our bears Teddy.

> I rip the head of mine. Does yours still have a head?

Better: a king orders three tankards of ale. Three tankards are

brought in on a silver platter. The first and the second are full

of ale but the last is empty.

What was the name of the king?

DrLaszloJamfa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 7
Elvis?
abu5ea at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 8
Philip the Third! *Larf*
DrLaszloJamfa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 9
Am I meant to be confused?!
abu5ea at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 10

> > You and I go to Toys R Us together and by two teddy bears.

> > We name both our bears Teddy. I rip the head of mine. Does yours

> still have a head?

>

> Better: a king orders three tankards of ale. Three tankards are

> brought in on a silver platter. The first and the second are full

> of ale but the last is empty.

>

> What was the name of the king?

Dunno; was that king waiting that table himself? Is his name "ale"?

kind regards,

Jos (stupid riddles; I need a Grolsch ;-)

JosAHa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...
# 11
> Philip the Third! > > *Larf*ROFL!Awesome.%
duffymoa at 2007-7-9 5:45:46 > top of Java-index,Java Essentials,Java Programming...