简体   繁体   中英

Efficiency with Type Hierarchy in Java

I have one parent class, which is abstract class for now, for four different subclasses at level two. For one of those subclasses, I would like to make a third level of subclasses which emulate two subclasses from level two. I don't want the class that will be the parent of them to be abstract or an interface, though, and I think just making it concrete wont be very efficient.

Edit: By efficient, I mean that in the given situation, are there any alternatives to the options mentioned above?

Is there any other efficient way to represent these two classes that will essentially be the same at level two? Because the explanation was a little ambiguous, here's a picture: http://i.imgur.com/mvCAS.png

Your question is not quite clear.

I don't want the class that will be the parent of them to be abstract or an interface, though, and I think just making it concrete wont be very efficient.

Well, if you don't want neither an abstract class, a concrete class, nor an interface, then I am afraid you are out of luck. Java offers no other choice in declaring / defining your types.

Moreover, Java does not allow multiple inheritance. That means if you want Column Major to be a subclass of both ColumnMajor Table and Table With Deactivate Cell , at least one of these must be an interface . And since in Java interfaces can only extend other interfaces, this means that Table must be an interface too.

It seems to me that you're mixing the concepts of capabilities and classification . You see, inheritance is not the right tool for everything. Try to make some of those classes compositions and not subclasses. For example, having "deactivate" cells seem to be a capability of a table, and maybe could be a separate class that can be composed in a table class to change how a table behaves. Take a look at the strategy and the state pattern.

In general it is better to configure a class by delegation instead of derivating more subclasses. Overusing inheritance is a typical behavior of oo beginners.

In your case the approach could be to create a class configuring the distinct behavior of your actual class set. If you design of such a class allows to be chainable you get a very flexibale, extendable architecture

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM