Can I hide or reduce access to “inherited methods” in Java?

I have a class structure. I want some methods in the base class to be accessible from classes derived directly from the base class, not from classes derived from derived classes According to the Java language specification, the access specification on the inheritance method can be overridden to make it more public, but not more private For example, this is what I need to do, but it 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 finish this?

The editor explained why I wanted to do this:

In this case, the class structure is a data processing and import structure It reads and parses text files of complete tabular data, and then stores them in the database

The base class is the base table class of the processing part of the management database It contains quite a lot of functions, which is common for all table types, because once they are in the database, they become unified

The intermediate class is specific to the type of table in the file to be parsed, and has table parsing and import logic It needs to access some database access functions of the base class

A top - level class is table - specific and can only initialize the layout of a table in a way that the parent class can understand Users of the base class do not need to view or access the database specific functions of the intermediate class In essence, I want to show these functions only on the base class, not anything else

I asked, because although the code I released is illegal, there may be other means to achieve the same outcome I asked if there was

Maybe hiding is the wrong way to illustrate this - what I really need to do is expose the private functions of some basic classes to one level in the hierarchy Hiding will do this - but I can see that hiding is a problem Is there another way?

Solution

I think the nature of your question exposes the conceptual problems of your object model When what you should actually do is describe a "have a" or "use" relationship, you are trying to describe all kinds of responsibilities as a "is a" relationship The fact that you want to hide the function of the base class from a subclass tells me that this problem does not actually map to the three-tier inheritance tree

It sounds like you are describing a classic ORM problem Let's see if we can remap it to other concepts instead of strictly "yes" inheritance, because I really think your problem is not technical, it is conceptual:

You say?

This may be clearer, but it sounds like we have a class that needs to manage database connections and common database operations After single responsibility, I think we can finish it here You do not need to extend this class, you need to give it to the class that needs to use its functions

The "middle class" here sounds a bit like data mapper This class does not need to extend the previous class. It needs to have its reference, perhaps injected into a constructor or a setter as an interface

I don't know why a high-level database schema knowledge (at least this is the phrase "initialization table layout" for me), but again, if the relationship between the first two classes is encapsulation ("has a" / "uses a") instead of inheritance ("is a"), I don't think it's a problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>