Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Personally I think it is a code smell.</p> <p>Like you say, unless they have some base functionality - which they don't, they should be abstract, forcing derived classes to provide implementation.</p> <p>If some derived classes shouldn't have an implementation for these methods, then there's probably something wrong with the design.</p> <p>Consider this:</p> <pre><code>public abstract class Animal { public abstract string Speak(); } public class Dog : Animal { public override string Speak() { Console.WriteLine("Woof"); } } public class Cat : Animal { public override string Speak() { Console.WriteLine("Meow"); } } </code></pre> <p>All fine so far, but what if you want to add an animal that doesn't speak?</p> <pre><code>public class Ant : Animal { public override string Speak() { // do nothing - ants don't speak. } } </code></pre> <p>This in my opinion is bad. Someone might do this (what you have described).</p> <pre><code>public abstract class Animal { public string Speak() { // not abstract because not all derived animals speak. } } </code></pre> <p>This in my opinion, is better, but still not great. What I would like to see in this situation is either Speak be moved to an interface and only the animals that can speak implement it, or something like this.</p> <pre><code>public abstract class Animal { } public abstract class Mammal : Animal { public abstract string Speak(); } public class Dog : Mammal { public override string Speak() { Console.WriteLine("Woof"); } } public class Cat : Mammal { public override string Speak() { Console.WriteLine("Meow"); } } public class Ant : Animal { } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload