• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Examulator Question

 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question No=646

Which of the following statements are true?

1 To be overridden a method must have the same name, parameter types and return type
2 It is possible to override methods in a superclass or the same class
3 classes that are marked as final may not be overriden
4 an overriding method cannot have more access (be more public) than the method being overriden
Answer is 1,3,and 4. But I don't agree with number 4. Maybe it should be "an overriding method cannot have less (be more private_ than the method being overriden.
Please advise.
 
Tom Adams
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right...the override cannot be more restrictive. The restrictive hiearchy least to most is...
1. public
2. protected
3. <default> - no access modifier
4. private

class ParentClass {
public void Foo() { }
private void Bar() { }
}
class ChildClass extends ParentClass {
private void Foo() { }// error = more restrictive
void Bar() { }// OK = less restrictive
}
[ February 13, 2003: Message edited by: Tom Adams ]
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maria,
You are correct.
I too solved that question yesterday, and stumbled upon it.
4th one is not correct.
Thanks
Mandar
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic