Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Java Rule Round-up Error?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(#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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


How do you override a private method?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!