aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help for method selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help for method selection" Watch "Need help for method selection" New topic
Author

Need help for method selection

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
1. public class X {
2. public X aMethod() { return this;}
3. }
1. public class Y extends X {
2.
3. }
Which two methods can be added to the definition of class Y? (Choose two)
A. public void aMethod() {}
B. private void aMethod() {}
C. public void aMethod(String s) {}
D. private Y aMethod() { return null; }
E. public X aMethod() { return new Y(); }

Ans - C,E

Hello ranchers i am not able to get this question . I think ans should be A,B.
Can some one expalin me

Source "whizlabs"


Thanks<br />Dinesh
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
can you give the reason why it should be A and B
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
The method is not taking String, its plain and simple method with no arguments passed to it.
In method C , String argument is passed.

Correct me if iam wrong.
Pls
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
A is incorrect because you are not overriding the method(return type is different)

B is incorect (same reason as above) and also you are restricting the method by private modifier.overridden method should not be more restrictive in the child class.

D is incorrect because of same reason as above

C is not overriding ,its overloading ,so perfectly valid

E is a valid overload with covariant returns
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
one correction in the above,
E is an OVERRIDE with covariant returns
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hi,
How 'C'(public void methoda(String s)) is valid, in overloading you must have same method name and different arguments. Argument is different but the method is not returning anything hwich original method does(public X methoda(return this .
Can you please shed some lights on this. I am getting confused much.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Originally posted by Dinesh Tahiliani:
Hi,
How 'C'(public void methoda(String s)) is valid, in overloading you must have same method name and different arguments. Argument is different but the method is not returning anything hwich original method does(public X methoda(return this)) .
Can you please shed some lights on this. I am getting confused much.
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Hi Dinesh,

in overloading you must have same method name but you should change both argument and return type.

thank you
-Mila-


SCJP 5
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
In overloading ,arguments should be different.Overloading method does not depend on the return type,it may or may not be same. so C is a valid answer.
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Hi Uma,

I read K&B book, for overload method, if we change the argument we should change the return type also. Please correct me if I'm wrong.

Thanks & regrds,
-Mila-
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
Hi Mila

There is a table in K & B 1.5 book giving "Difference between Overloaded and Overridden method" in chapter 2 or 3 i think.There it says clearly that for overloaded method return type CAN CHANGE.But for overridden method "Must not change".For Overloaded method area of concern is only argument list and nothing else.Hope its clear.
sitaram
Greenhorn

Joined: Jan 24, 2008
Posts: 26
Hi,

1. public class X {
2. public X aMethod() { return this;}
3. }
1. public class Y extends X {
2.
3. }
Which two methods can be added to the definition of class Y? (Choose two)
A. public void aMethod() {}
B. private void aMethod() {}
C. public void aMethod(String s) {}
D. private Y aMethod() { return null; }
E. public X aMethod() { return new Y(); }

Ans - C,E


A.overriden X.aMethod
The return type is incompatable with X.aMethod().
B.The return type is incompatable with X.aMethod().
C.aMethod is not overriden.
D.The return type is incompatable with X.aMethod().
E.Overriden X.aMethod.The return type is compatable with X.aMethod().

Regards,
D.Sitaramayya.
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Thank you Uma..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help for method selection