• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java ranch mock

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a method with no access modifier be overriden by a method marked protected?
Java ranch says it is True.
But I say it is "false".
A method can over ride another method if its access modifier is same or less restrictive than the overriden method.
Some one plz clarify.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Ranch is right
and so is the statement you made!
protected is LESS restricted the the DEFAULT (no thing written) modifier.
default allows only classes that in the same package to access the method/varuiable.
protected allows all the classes in the same package AND if they inherit from that class.
 
Raj Hans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pradeep bhat:
Can a method with no access modifier be overriden by a method marked protected?
Java ranch says it is True.
But I say it is "false".
A method can over ride another method if its access modifier is same or less restrictive than the overriden method.
Some one plz clarify.

The method with no modifier is more restrictive than the method marked protected..
e.g.
// supper class
package sample;
public class testObj
{
protected void tstMethod(int i){

}
}
//sub class
package sample;
class tstObj2 extends sample.testObj
{
void tstMethod (int i) {

}

}
error in subclass : access modifier more restrictive.
HTH
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Dark Maul"
Welcome to the JavaRanch! Please adjust your displayed named to match the JavaRanch Naming Policy. As much as we all enjoy Star Wars, please choose one that is not "Obviously fictitious".
You can adjust it here.
Thanks! and welcome to the JavaRanch!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic