Interfaces and abstract classes help organize code by defining rules for other classes. Choosing between them helps keep your program clear and easy to change.
Interface vs abstract class decision in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
public interface IExample { void DoWork(); } public abstract class AbstractExample { public abstract void DoWork(); public void CommonMethod() { Console.WriteLine("This is shared behavior."); } }
An interface only declares method signatures without any implementation.
An abstract class can have both abstract methods (without body) and concrete methods (with body).
public interface IWorker { void Work(); } // Empty interface example public interface IEmpty {} // Interface with multiple methods public interface IAdvancedWorker { void Work(); void Rest(); }
public abstract class WorkerBase { public abstract void Work(); public void Rest() { Console.WriteLine("Resting..."); } } // Abstract class with only abstract methods public abstract class PureAbstract { public abstract void DoSomething(); }
// Edge case: Interface with default implementation (C# 8+) public interface IDefaultWorker { void Work(); void Rest() => Console.WriteLine("Default rest"); }
This program shows a dog class that inherits an abstract class and implements an interface, and a cat class that only implements the interface. Dog can use shared code from the abstract class, but cat cannot.
using System; public interface IAnimal { void Speak(); } public abstract class AnimalBase { public abstract void Speak(); public void Eat() { Console.WriteLine("Eating food."); } } public class Dog : AnimalBase, IAnimal { public override void Speak() { Console.WriteLine("Woof!"); } } public class Cat : IAnimal { public void Speak() { Console.WriteLine("Meow!"); } } class Program { static void Main() { Dog dog = new Dog(); Cat cat = new Cat(); Console.WriteLine("Dog says:"); dog.Speak(); dog.Eat(); Console.WriteLine("Cat says:"); cat.Speak(); // cat.Eat(); // Not available because Cat does not inherit AnimalBase } }
Time complexity: Choosing interface or abstract class does not affect runtime performance directly.
Space complexity: Both add no extra memory overhead beyond normal class inheritance.
Common mistake: Trying to add fields or method implementations in interfaces before C# 8, which is not allowed.
Use interfaces when you want to define capabilities without implementation. Use abstract classes when you want to share code and force some methods to be implemented.
Interfaces define what methods a class must have but do not provide code.
Abstract classes can provide some shared code and force subclasses to implement others.
Choose interfaces for unrelated classes that share behavior, and abstract classes for related classes sharing code.
Practice
interface instead of an abstract class in C#?Solution
Step 1: Understand interfaces
Interfaces define method signatures without implementation, so they are ideal for unrelated classes that share behavior but not code.Step 2: Understand abstract classes
Abstract classes can provide shared code and force subclasses to implement certain methods, so they are better for related classes sharing code.Final Answer:
Use an interface when unrelated classes share behavior but do not share code. -> Option BQuick Check:
Interface = unrelated classes sharing behavior [OK]
- Thinking abstract classes can't have implemented methods
- Using abstract class for unrelated classes
- Confusing interfaces as code providers
Solution
Step 1: Recall abstract class syntax
In C#, the keywordabstractprecedesclass, followed by the class name and method declarations.Step 2: Check each option
abstract class Vehicle { public abstract void Move(); } correctly uses "abstract class Vehicle" and declares an abstract method. Options A, C, and D have incorrect keyword order or use interface syntax.Final Answer:
abstract class Vehicle { public abstract void Move(); } -> Option AQuick Check:
abstract class syntax = "abstract class" [OK]
- Swapping 'class abstract' instead of 'abstract class'
- Using interface syntax for abstract class
- Missing 'abstract' keyword before method
interface IAnimal { void Speak(); }
abstract class Mammal { public void Breathe() { Console.WriteLine("Breathing"); } public abstract void Speak(); }
class Dog : Mammal, IAnimal { public override void Speak() { Console.WriteLine("Woof"); } }
var dog = new Dog();
dog.Breathe();
dog.Speak();What will be the output when this code runs?
Solution
Step 1: Understand class and interface usage
The classDoginherits from abstract classMammaland implements interfaceIAnimal. It overridesSpeak()and inheritsBreathe().Step 2: Trace method calls
Callingdog.Breathe()prints "Breathing". Callingdog.Speak()prints "Woof" as overridden inDog.Final Answer:
Breathing Woof -> Option AQuick Check:
Abstract class method + override = correct output [OK]
- Thinking multiple inheritance causes error in C#
- Confusing order of output lines
- Missing override keyword causing compile error
abstract class Shape {
public abstract void Draw();
}
class Circle : Shape {
public void Draw() {
Console.WriteLine("Drawing Circle");
}
}Solution
Step 1: Identify abstract method implementation rules
When a class inherits an abstract method, it must override it using theoverridekeyword.Step 2: Check Circle class method
TheDraw()method inCircleis declared aspublic void Draw()but missingoverride, causing a compile error.Final Answer:
Circle must declare Draw() as override, not just public. -> Option DQuick Check:
Override abstract method = must use 'override' keyword [OK]
- Omitting 'override' keyword on abstract method implementation
- Thinking abstract classes can't have abstract methods
- Assuming static needed for overridden methods
Log() but also share some common logging code. Which approach is best in C#?Solution
Step 1: Analyze requirements
Multiple unrelated classes must implementLog()and share some common code.Step 2: Choose interface and abstract class combination
Interfaces allow unrelated classes to share method signatures. Abstract classes can provide shared code. Classes can implement interface and inherit abstract class to get both.Step 3: Evaluate other options
Using only abstract class limits inheritance to related classes. Using only interface lacks shared code. Concrete class inheritance limits flexibility.Final Answer:
Create an interface ILogger with Log() and a separate abstract class with shared code, then have classes implement both. -> Option CQuick Check:
Interface + abstract class = behavior + shared code [OK]
- Trying to use only abstract class for unrelated classes
- Ignoring shared code needs
- Assuming multiple inheritance of classes is allowed
