Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock question help!!!!!

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question below is not clear to me. Does the question relate
to overriding. It does not look so to me as the answers are 1 and 3.
Another question : Can the subclass overload the base class methods.
If a base class has a method defined as
void method() { }
Which of the following are legal prototypes in a derived class of this class. Select all correct answers.
1. void method() { }
2. int method() { return 0;}
3. void method(int i) { }
4. private void method() { }
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO the answer is 1,3.
1 is overridding totally without changing access modifier and is allowed
2 is false because it is only changing the return type.
3 is overloading because it changes the arugment list
4 is false an overridding method cannot change the access modifier to be more private.
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,there:
yes, methods can be overloaded both in subclass and base class, not only in subclass.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks carl and helen!!!
i have a question derived out of ur answers.
so the question is about overriding and the answer 2 becomes a totally different method in a subclass. Is answer 3 not a new method in the subclass as it has a argument.
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,there:
/////////////////////////////////////////////////
i have a question derived out of ur answers.
so the question is about overriding and the answer 2 becomes a totally different method in a subclass. Is answer 3 not a new method in the subclass as it has a argument.
////////////////////////////////////////////////
Answer 2 is not legal, because only with different return type are not considered as Overloading or Overriden. The compiler will not let you pass if you write code like answer 2.
Answer 3 is not a new method in the subclass as it has a argument but it is the overloading version of base class.
Feel free to conrect me. Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi helen,
According to ur explanation if I modify answer 2 by putting in an arguement as below. Now, can answer 2 be the right answer?
2. int method(String s) { return 0;}
let me know ur thoughts on this.
thanks
Rakhee Arun
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic