Access specifiers and code reuse in java

How do access specifiers such as private, public, etc., help in code reuse?

I had this interview question and could not find ans. Plz help me.

+4
source share
4 answers

Around this, there are a few subtle lines with which I could answer in several different ways.

Consider the case where the developer marks almost everything as personal. Perhaps this was a great class that could be reused if I could extend it and override the doSomething() method. However, since the method was marked as personal, my hands are tied. This will force many developers to copy and paste the entire class, changing one necessary part. (Not cool.) So you can see that labeling everything as private prevents reuse.

However, on the other hand, consider the case where the developer marks everything as public. The developer or other developers begin to write extension classes or call all the various methods / access to attributes, which may need to be marked as protected / private. A design error was found, or one that requires the class to be changed. However, since it was written so “openly,” it may have been a simple solution, now much more complicated, since all additional links must be found and reviewed.

I think the best option is a compromise. Mark those that are intended to be used by client code as "public." Note those things that do not have a business that are expanding / redefined as "private". If there is something that might prove useful to someone in the future, it should be designated as “protected.” And always add Javadocs for further notification of your intentions.

+4
source

Code that needs to be reused must be clear and reliable.

It’s clear: when creating some private methods, the author of the class indicates that these methods are internal implementation desaturations, the repeated user should take into account only public methods.

Reliability: by making private fields, we prevent the user from reusing the reuse of the internal state of reusable code.

+1
source

Assuming that you are well aware of the scope of these access specifiers, such as

private class only

default only

protected classes and derived classes

public

Now imagine that you have a class containing a method

 public int calculate(int x,int y,int z){ z=x+y; x=z+x; z=x+y; return z; } 

This is a public method and can be accessed from anywhere, from any package, and from any class, whether it is derived or not. Now you want to use this function in a new class, you can simply import this class and use this method without writing it again and again.

This is a very simple example, which you can replace with your own function here, for example, checking a series - is it a series of Fibonacci or not, calculating simple interest / interest, etc. and reuse it where you need it.

0
source

Suppose I have a utility utility class that I want someone else to reuse. I have many methods and variables in this class, but people using this class do not need them.

  • public denotes a method that can be easily repeated. It should always be well-documented so that someone knows how and when to call it.

  • private marks the member as part of the internal mechanism - my repeat users do not need to worry about this.

Explicitly about an access model that allows you to more clearly understand how to access this code, and therefore make it more convenient and convenient for others to reuse.

In the interview (and I hired many developers), you should clearly explain the purpose of each access modifier and give an example of each use.

0
source

Source: https://habr.com/ru/post/1435431/


All Articles