O
O
Oleg Chinakin2019-03-29 09:53:07
Java
Oleg Chinakin, 2019-03-29 09:53:07

How to hide a Java class method?

Actually, how to hide / make some class methods not available?
For example, overwritten public methods of the parent class:

public class MyLayout extends FrameLayout {

    public MyLayout(Context context) {
        super(context);
    }

    /**
     * КАК СДЕЛАТЬ ЭТОТ МЕТОД НЕДОСТУПНЫМ / ИЗМЕНИТЬ ВИДИМОСТЬ ???
     */
    @Override
    public void addView(View child) {
        super.addView(child);
    }
}

In C#, I saw that they did something like this:
//Вот это
public override string ToString() {}

//Меняли на это
private new string ToString() {}

I can't find how to do this in java. Moreover, in Android Studio, in library classes, for example, Context, there are hidden methods that look like this:
/** @hide */
public boolean isAutofillCompatibilityEnabled() {
    return false;
}

And, accordingly, this method is not visible, although it is public.
Hence the questions: how is it done and how to do the same?
And what are these tags in comments (@hide, {@link }, etc.)? If I write the same way, an unknown tag says.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Varakosov, 2019-03-29
@SaintRepublic

Within Java, you cannot change the visibility of an inherited method. There are several options. First, you can leave the method override empty and move the functionality to another place. Second, to do it right, you need extra encapsulation, so use the tools of the language, create an interface that offers only the necessary methods.
Annotations in comments, inside the Android API, have a different meaning. For example, it @hidesays that the method was not designed to be used outside of the API.

A
Alexey2222, 2019-03-29
@Alexey2222

As a last resort, you can just make it empty. That is, with an empty body.

E
Evgeny Surin, 2019-04-01
@jenyaatnow

I would like to add that in java it is still possible to change the visibility of an inherited method, but only towards the extension. It is really impossible to narrow down - this would be a violation of the principle of Barbara Liskov.
And the tags in the comments are javadoc markup. And such comments are not comments at all, but documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question