File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding access visibility rules

 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a super class given method
public void aMethod(){}

In a subclass giving the only visibility is
public void aMethid(){}

Like that,
In a Super class,a method with protected---subclass,a method with protected/public is valid.giving private or default is error

In a Super class,a method with private--no overriding in subclass

In a Super class,a method with default-subclass,a method with protected/public/default is valid. giving private is error

I checked every of the above with my compiler.still I need to confirm from anyone because consider the following question.


Given the code below,and making no other changes,which access modifiers ar placed before myMethod() on line 3

class Super{
void myMethod(){//line 3

}
}

class Q extends Super{
void myMethod(){//line 7

}
}

Given options:
1.public
2.protected
3.private
4.none of the above

Given 1,2,3 answers are correct.since private feature is accessible only to its own class,selecting private is also valid.

But my answer is only giving private on line 3 would work.of course ,line 7 is not overridint the super class method.that is a separate method.How come giving public/protected on line 3 is valid for the above program.please help me and correct me if my beginning rules are not correct.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are allowed to increase the visibility of overridden methods, but you can't decrease the visibility.
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply keith. is the three answers correct pr wrong?.....please give more details depends on the above program
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The given answer 1,2,3 is incorrect; only line 3 is correct, for the reason you gave. Wherever you got this question from, it's not a trustworthy source; ignore it.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic