Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.334 :

Posted: Fri May 15, 2015 8:07 pm
by mundrapiyush
lass TaskBase
{
int getStatusCode(Object obj) throws NullPointerException
{
if(obj != null ) return 1;
else return 0;
}
}
class ParallelTask extends TaskBase
{
//override getStatusCode method.
}

Option: 1 Overriding method can take String as a parameter.

String is the child of Object class. Cant' we pass subtype as parameter while overriding. If not why so. Please explain.
Thanks.

Re: About Question com.enthuware.ets.scjp.v6.2.334 :

Posted: Fri May 15, 2015 9:10 pm
by admin
No, you can't do that. If Java allowed that what would happen to the users of the class that try to pass it objects that are not Strings. For example,

Code: Select all

void someMethod(TakeBase b)
{
   b.getStatusCode(new Integer(0)); //if b refers to an object of SubClass (which overrides the getStatusCode method with String instead of Object, how will this call work??
}

...

TaskBase b = new SubClass();
somemethod(b); 
This should have been explained in regular Java book. Which book are you following?

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.334 :

Posted: Fri May 15, 2015 10:53 pm
by mundrapiyush
kathy sierra.

I got my mistake now. Thanks.