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
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.
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.
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?
> 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?
> > 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 ;-)