Is it possible to hide or lower access to Inherite

2019-02-21 09:42发布

I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from derived classes. According to the Java Language specification it is possible to override access specifications on inherited methods to make them more public, but not more private. For example, this is the gist of what I need to do, but is illegal:

// Defines myMethod
public class Base {
    protected void myMethod() {}
}

// Uses myMethod and then hides it.
public class DerivedOne extends Base {
    @Override
    private void myMethod();
}

// can't access myMethod.
public class DerivedTwo extends DerivedOne {

}

Is there any way to accomplish this?

Edited to explain why I would like to do this:

In this case the class structure is a data handling and import structure. It reads in and parses text files full of tabular data and then stores them in a database.

The base class is the base table class managing the database handling part of it. There is a fair amount of functionality contained in it that is common to all table types - as once they are in the database they become uniform.

The middle class is specific to the kind of table in the file being parsed, and has the table parsing and import logic. It needs access to some of the base class's database access functions.

The top level class is specific to the table and does nothing more than initialize the table's layout in a way the parent classes can understand. Also users of the base class do not need to see or access the database specific functions which the middle class do. In essence, I want to reveal these functions only to one level above the base class and no one else.

I ask because, although the code I posted as an example is illegal, there may be some other means to accomplish the same end. I'm asking if there is.

Perhaps hiding is the wrong way to phrase this - what I really need to do is expose some functionality that should be private to the base class to the class one level up in the hierarchy. Hiding would accomplish this - but I can see how hiding would be a problem. Is there another way to do this?

8条回答
beautiful°
2楼-- · 2019-02-21 10:35

If you did this then DerivedOne would not be a Base, from the DerivedTwo's point of view. Instead what you want is a wrapper class

//Uses myMethod but keeps it hidden
public class HiddenBase {
    private final Base base = new Base();
    private void myMethod();
    public void otherMethod() {base.otherMethod();}
}

You can't access protected methods of the base though this way...

查看更多
唯我独甜
3楼-- · 2019-02-21 10:36

When overriding a method you can only make it more public, not more private. I don't know why you use the word "general"

Remember that, ordering from least to most restrictive:

public<protected<default<private

Yes, "protected" is a less restrictive access modifier than default (when no modifier is used), so you can override a default method marking the overriding method as protected, but not do the opposite.

Can: You can override a protected method with a public one.

Can't: You can't override a public method with a protected one.

查看更多
登录 后发表回答