• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help for method selection

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Ajay Singh
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give the reason why it should be A and B
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one correction in the above,
E is an OVERRIDE with covariant returns
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh,

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

thank you
-Mila-
 
Ajay Singh
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Uma..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic