public abstract class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine("Animal is eating");
}
public abstract void MakeSound(); // must be implemented in derived class
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark!");
}
}
Usage:
Animal dog = new Dog();
dog.Eat(); // Inherited concrete method
dog.MakeSound(); // Overridden method
Example: Interface
public interface IPlayable
{
void Play();
}
public interface IStoppable
{
void Stop();
}
public class MediaPlayer : IPlayable, IStoppable
{
public void Play() => Console.WriteLine("Playing...");
public void Stop() => Console.WriteLine("Stopped.");
}
Usage:
IPlayable player = new MediaPlayer();
player.Play();
When to Use What?
Scenario
Use
Need to provide base functionality
Abstract class
Need to support multiple base types
Interface
Design a plug-in system or API contract
Interface
Need constructors or fields
Abstract class
Want to share default code
Abstract class (or C# 8+ interfaces)
Tip
Use interface when you only need contract/behavior
Use abstract class when you need to share logic and structure
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Quick Comparison Table
Example: Abstract Class
Usage:
Example: Interface
Usage:
When to Use What?
Tip