- Home /
Can abstract classes inherit abstract methods without implementing them?
If I have an abstract class similar to the one below:
public abstract class Foo : Monobehaviour
{
public abstract void DoSomething ();
}
and another abstract class (we'll call it "Bar") that inherits from Foo, should I be able to override DoSomething and keep it abstract? That is, I want a class that inherits from Bar to have to implement DoSomething, but I don't want Bar itself to implement the method (even with an empty body, because then a class that inherits from Bar wouldn't be forced to implement it). The MSDN docs for inheritance imply that it should be doable when they say "Derived classes that are not abstract themselves must provide the implementation for any abstract methods from an abstract base class[,]" but don't state so explicitly. Writing
public abstract class Bar : Foo
{
public abstract override void DoSomething ();
}
still generates a 'Bar' does not implement inherited abstract member 'Foo.DoSomething()'
error. Is this an error on my part, or is something wonky with Mono?
Answer by SteveFSP · Aug 10, 2010 at 02:36 PM
Keeping with your example: Just remove the declaration of DoSomething() from the Bar class. Since the class is declared as abstract, it doesn't need to implement any abstract methods from its base class.
public abstract class Foo : MonoBehaviour { public abstract void DoSomething (); }
public abstract class Bar : Foo { // No declaration of DoSomething(); // Just add new methods. }
Thanks! You might edit "$$anonymous$$onobehaviour" to "$$anonymous$$onoBehaviour."
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
The best way to create instances of an enemy class 2 Answers
Animation Event (Function not supported) for a public overridden function 0 Answers
How would I get different references to scripts depending on what object it is attached to? 1 Answer
How to work with a list of base class, if a variable is only found in some subclasses? 2 Answers