Overriding abstract methods in a inherited abstract class

Okay, so basically I have the following problem: I'm trying to have an abstract class inherit from another abstract class that has an abstract method, but I don't want to implement an abstract method in either of them, because the third class inherits from both of them:

public abstract class Command { public abstract object execute(); } public abstract class Binary : Command { public abstract object execute(); //the issue is here } public class Multiply : Binary { public override object execute() { //do stuff } } 

I am trying to separate binary commands from unary commands, but I don’t want / cannot implement the execute method anyway. I thought that Binary overrides the abstract method (as needed) and then just throws an unrealized exception. If I redefine it, then I must declare the body, but if I make it abstract, then I "hide" the inherited method.

Any thoughts?

+8
inheritance c # abstract-class abstract-methods
source share
2 answers

You do not need to declare execute() in a binary class, as it is already inherited from Command. Abstract methods should not be implemented by other abstract classes - the requirement is passed to the final concrete classes.

 public abstract class Command { public abstract object execute(); } public abstract class Binary : Command { //the execute object is inherited from the command class. } public class Multiply : Binary { public override object execute() { //do stuff } } 
+14
source share

Just omit the execute() declaration in Binary in general. Since Binary also abstract, you do not need to implement the abstract methods of its ancestors.

+3
source share

All Articles