0

When I try to access and override an inherited abstract function from an abstract class which is of protected access level,

protected <ReturnDataType> <FunctionName>() {

upon removing the Access Type Protected, Eclipse throws me this error

Cannot reduce the visibility of the inherited method from the < inherited class >

which is resolved with the quickfix of giving it the accesstype Protected.

I know that, I can also have the access type as Public for this overrided function.

Now, my question is:: What is the risk / issue with me having this function as Public?

ItsMeGokul
  • 403
  • 1
  • 5
  • 16
  • I'm not sure "risk" figures into it. I think it's just whether you want any other method in the system to be able to access your method, or if you want to only allow access by classes which inherit from your class. It's entirely up to you. – markspace Jan 22 '18 at 16:17
  • It is difficult to generically assess risk. It depends on the architecture of your apps and what you need/want exposed. In general you want to keep public members to a minimum. Any public method can easily be consumed in ways you never intended, possibly expose data that shouldn't be exposed, and increase a possible attack vector on your system. Beyond that, i'm not sure how to assess risk of your specific app. – tatmanblue Jan 22 '18 at 16:20
  • You can find the best explanation of the problem in the famous Effective Java book Item 15: Minimize the accessibility of classes and members. – Boris Jan 22 '18 at 16:24

2 Answers2

0

It is a means to help protect your code against errors. There exists coding languages without encapsulation. At their most extreme, any code can change any piece of any data and we may simply not want that to be possible. A disciplined coder will reduce the number of places a given type of data is manipulated, but it may still not be obvious all the combinations of operations that could leave objects* in different states. The situation gets worse when their code is then used as part of someone else's code. So the risks of changing these accessTypes is that it can cause errors down the line if used improperly.

Cowboy Farnz
  • 329
  • 2
  • 11
  • 1
    I think you'll find better insight into why this isn't allowed in the duplicate question. Your answer's close, but not quite there. – Makoto Jan 22 '18 at 16:31
-1

All public does is allow the variable to be accessed through another package, so if someone imported your project as a external library into their project they would be able to access a global variable. This page may be of some use to you: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Alex Finch
  • 34
  • 1
  • 7