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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mundrapiyush
Posts: 3
Joined: Fri May 15, 2015 8:04 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

mundrapiyush
Posts: 3
Joined: Fri May 15, 2015 8:04 pm
Contact:

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

Post by mundrapiyush »

kathy sierra.

I got my mistake now. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests