Is it possible to overload abstract methods in an abstract Java class, but implement only one of the overloaded methods in subclass?

huangapple go评论98阅读模式
英文:

Is it possible to overload abstract methods in an abstract Java class, but implement only one of the overloaded methods in subclass?

问题

I understand that you want me to translate the provided code snippet into Chinese. Here it is:

我有一个抽象类(仅显示相关部分),其中有两个重载方法。

abstract public class Component {
    ...
    abstract protected void createPhysics();
    abstract protected void createPhysics(Comp1D[] comp1DS);
    ...
}

在扩展此抽象类的子类中,我只想使用带参数的方法或不带参数的方法,但从不同时使用两者。例如:

public class Comp1D extends Component{
    ...
    protected void createPhysics(Comp1D[] comp1Ds){
        ...
    }
}

以及

public class Comp3D extends Component{
    ...
    protected void createPhysics(){
        ...
    }
}

当然,这样写无法通过编译,因为子类中未实现另一个createPhysics方法。我草率的解决方案是在子类中都实现这两个方法,但未使用的方法将具有空的方法体。

在Java 8中是否有更优雅的解决方法呢?

英文:

I have an abstract class (showing only the relevant parts) with two overloaded methods.

abstract public class Component {
	...
    abstract protected void createPhysics();
    abstract protected void createPhysics(Comp1D[] comp1DS);
	...
}

In the subclasses which extend this abstract class I only want to use either the one with arguments or the one without, but never both of them. For example

public class Comp1D extends Component{
	...
	protected void createPhysics(Comp1D[] comp1Ds){
		...
	}
}

and

public class Comp3D extends Component{
	...
	protected void createPhysics(){
		...
	}
}

Of course this won't compile this way since the other createPhysics method is not implemented in the subclass. My quick and dirty solution would be to implement both methods in subclasses, but the unused method would have empty body.

Is there a more elegant way to solve it in Java 8?

答案1

得分: 3

使用抽象方法是没有问题的。在语法层面上,这也不会有问题。如果有一个 Component,就可以同时调用两种方法。但是如何知道哪个方法被实现了,哪个方法没有被实现呢?

可以在抽象类中定义这两种方法,然后让它们抛出例如 UnsupportedOperationException,从而强制子类覆盖(至少其中的一个)方法,如果它们希望不抛出这样的异常。然而,这似乎是为另一个问题找到的一种变通方法。

我建议重新评估该部分的整体架构,并找到另一个解决该问题的方法。例如,也许将这两种方法放在两个分离的类中,并为这些类编写处理程序,会产生一个更清晰的架构。

英文:

With abstract methods, there is not. And on a syntactical level, it would not be sound either. If one has a Component, one can call both methods. How should one know which one is implemented and which one is not?

One could define both method in the abstract class and let them throw, for example, an UnsupportedOperationException, thus forcing sublcasses to override (at least one of) those methods if they wish to not throw such an exception. This, however, seems like a workaround for another problem.

I would suggest re-evaluating the overall architecture of that section and find another solution to the problem. For example, maybe two separated classes and handler for those classes would yield a cleaner architecture.

答案2

得分: 2

问题是,为什么你想在这里使用抽象类。如果你打算使用一个带有默认实现的接口,那怎么办。你可以实现该接口并仅覆盖所需的方法。

英文:

The question is, why do you want to use an Abstract class here. What if you plan to use an interface, with default implementations. You can implement the interface and override only the required method

答案3

得分: 2

Abstract class的概念是在你想要在类中定义共同的方法签名,并强制子类为这些方法提供实现时使用的。从这个角度来看,你尝试实现抽象类的方式并不太合理。

你还可以使用抽象类来定义一个基础类型,以支持面向对象的特性,如多态和继承,我认为这可能是你想要做的。如果是这种情况,我建议声明一个没有抽象方法的抽象类,或者声明一个带有这两个方法默认实现的接口,然后你可以在实现类中进行重写。

英文:

The idea of using abstract class is when you want to define common method signatures in the class and force sub-classes to provide implementation for such methods. From this point of view the way you are trying to implement abstract class doesn't make much sense.

You can also use abstract class to define a base type to support O-O features like polymorphism and inheritance and i think this is what are you trying to do .
If this is the case i suggest to declare an abstract class without abstract methods or declare an interface with default implementation for both methods and then you can override in implementation classes.

答案4

得分: 1

正如@Turning85所指出的,这样的实现并没有太多意义。

要么你希望给你的successor类灵活性,根据它们自己的特定需求实现这两种方法,要么你希望从它们身上消除这种复杂性,并在抽象类中实现整个逻辑,其中你可以像这样做:

abstract class Component() {

    protected void createDefaultPhysics() {
        // 实现
    }

    abstract protected void createPhysics(Comp1D[] comp1DS);

}

然后你的具体类:

public class Comp1D extends Component{

      protected void createPhysics(Comp1D[] comp1Ds){
           if(comp1Ds == null) {
                createDefaultPhysics();
           }
      }
}
英文:

As @Turning85 pointed out, such an implementation would not make much sense.

Either you want to give your successor classes the flexibility to implement both of the methods according to their own specific needs or you want to take this complexity away from them and implement the whole logic in the abstract class, where you could have something like this:

abstract class Component() {

    protected void createDefaultPhysics() {
        //implement
    }

    abstract protected void createPhysics(Comp1D[] comp1DS);

}

and your concrete classes:

public class Comp1D extends Component{

      protected void createPhysics(Comp1D[] comp1Ds){
           if(comp1Ds == null) {
                createDefaultPhysics();
           }
      }
}

huangapple
  • 本文由 发表于 2020年8月14日 21:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63413274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定