wood burning stoves 2.0*
The moose likes Mock Exam Errata and the fly likes Java Rule Round-up Error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Java Rule Round-up Error?" Watch "Java Rule Round-up Error?" New topic
Author

Java Rule Round-up Error?

Yongtao You
Greenhorn

Joined: Oct 18, 2004
Posts: 1
(#56) Can a method with no access modifier be overriden by a method marked protected?

Answer: Yes
Overriden methods are allowed to have LESS restriction, and since protected is less restricted than default (package), this is allowed.

--------

This is an error, right? Here is another one:

--------

(#141) an overriding method can change the access modifier from default to protected.

Answer: true

---------

Error?
[ October 18, 2004: Message edited by: Yongtao You ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Both answers are correct. Try writing some code to see. What happens if you create a method with no modifier (default access), and try to override it with protected access? Does it work, or not?


"I'm not back." - Bill Harding, Twister
Georg Nieuwoudt
Greenhorn

Joined: Nov 02, 2004
Posts: 13
This might clear up your sceptisism :
PRIVATE -> DEFAULT -> PROTECTED -> PUBLIC

a private method may be overridden by a default, protected or public method
a default method may be overridden by a protected or public method
a protected method may be overridden by a public method
and public may be overridden by a public method

reversing the process wont work other wise you will get an error msg "method cannot be more private"(something like that anyways) at compile time


[SCJP 1.4]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
a private method may be overridden by a default, protected or public method

How do you override a private method?


Mike Gershman
SCJP 1.4, SCWCD in process
Rajith Vidanaarachchi
Greenhorn

Joined: Nov 06, 2004
Posts: 11
Hello,
You can give a less restricted access modifier when overriding.You can prove this by writing some code.So both are correct.
I hope you'll understand.
[ December 20, 2004: Message edited by: Rajith Vidanaarachchi ]
Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Ranjit, Mike is right - you cannot override a private method even thought it may appear that you are overriding it - in reality a private method is restricted to the class in which it is defined as it cannot be inherited - well now another q can you override a static method
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I just tried and you can override a static method. However the 'correct' way to call a static method would be

MyClass.staticMethod();

rather than

MyClass myClass = new SubClass();
myClass.staticMethod();

so if it is ever a problem you should probably be slapped.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Rule Round-up Error?