this wont work
public static void doWork(Person person) {
// Item 3
if(person instanceof Person.User){
Person.User user = new Person.User();
user.live();
}
else if(person instanceof Person.Loser){
Person.Loser user = new Person.Loser();
user.doNothing();
}
else if(person instanceof Person.Coder){
Person.Coder user = new Person.Coder();
user.writeCode();
}
else if(person instanceof Person.Programmer){
Person.Programmer user = new Person.Programmer();
user.enjoy();
}
}
_______________________________________________________________________
but this works
public static void doWork(Person person) {
if(person instanceof Person.User)
{
((Person.User)person).live();
}
else if(person instanceof Person.Loser)
{
((Person.Loser)person).doNothing();
}
else if(person instanceof Person.Coder)
{
((Person.Coder)person).writeCode();
}
else if(person instanceof Person.Programmer)
{
((Person.Programmer)person).enjoy();
}
}
Krunoslav Krainović
Level 16
One solution works and other not,but i get the same result
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
13 November 2019, 18:08
The tasks says specifically that the object passed in needs to have the proper method called on it. The first method does not call the method on the object passed in but instead creates a new object and calls the method.
0