Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Mock question help!!!!!

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() { }
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you.
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic