Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an interface in C#?
An interface is a contract that defines a set of methods and properties without implementation. Classes or structs that implement the interface must provide the implementation.
Click to reveal answer
beginner
What is an abstract class in C#?
An abstract class can have both implemented and unimplemented methods. It cannot be instantiated directly and is meant to be a base class for other classes.
Click to reveal answer
intermediate
When should you choose an interface over an abstract class?
Choose an interface when you want to define a contract for unrelated classes or when multiple inheritance of behavior is needed, since a class can implement multiple interfaces.
Click to reveal answer
intermediate
When is an abstract class a better choice than an interface?
Use an abstract class when you want to share common code among closely related classes and provide some default behavior along with abstract methods.
Click to reveal answer
beginner
Can a class implement multiple interfaces or inherit multiple abstract classes in C#?
A class can implement multiple interfaces but can inherit only one abstract class.
Click to reveal answer
Which of the following can a C# class inherit from multiple times?
AConcrete classes
BAbstract classes
CInterfaces
DStructs
✗ Incorrect
In C#, a class can implement multiple interfaces but can inherit only one abstract or concrete class.
What is a key difference between an interface and an abstract class?
AAbstract classes can have method implementations, interfaces cannot
BInterfaces can have method implementations, abstract classes cannot
CInterfaces can be instantiated, abstract classes cannot
DAbstract classes cannot have fields, interfaces can
✗ Incorrect
Abstract classes can have implemented methods and fields, while interfaces cannot have fields and traditionally had no implementations (though default interface methods exist in newer C# versions).
When would you prefer an abstract class over an interface?
AWhen you want to share common code among related classes
BWhen you want multiple inheritance of behavior
CWhen you want to define a contract without implementation
DWhen you want to implement unrelated behaviors
✗ Incorrect
Abstract classes allow sharing common code and default behavior among related classes.
Can interfaces contain fields in C#?
AYes, interfaces can have fields
BOnly private fields are allowed in interfaces
COnly static fields are allowed in interfaces
DNo, interfaces cannot have fields
✗ Incorrect
Interfaces cannot contain fields; they only declare methods, properties, events, or indexers.
Which statement is true about abstract classes?
AThey can be instantiated directly
BThey can contain both abstract and concrete methods
CThey must implement all interface methods
DThey cannot have constructors
✗ Incorrect
Abstract classes can have both abstract methods (without implementation) and concrete methods (with implementation).
Explain the main differences between interfaces and abstract classes in C# and when to use each.
Think about code sharing and inheritance rules.
You got /5 concepts.
Describe a real-life scenario where choosing an interface is better than an abstract class.
Consider when different objects share capabilities but not implementation.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes when to use an interface instead of an abstract class in C#?
easy
A. Use an interface when you want to provide shared code to subclasses.
B. Use an interface when unrelated classes share behavior but do not share code.
C. Use an abstract class when unrelated classes share behavior but do not share code.
D. Use an abstract class only when no methods need to be implemented.
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 B
Thinking abstract classes can't have implemented methods
Using abstract class for unrelated classes
Confusing interfaces as code providers
2. Which of the following is the correct syntax to declare an abstract class in C#?
easy
A. abstract class Vehicle { public abstract void Move(); }
B. interface Vehicle { void Move(); }
C. class abstract Vehicle { public void Move(); }
D. abstract Vehicle class { void Move(); }
Solution
Step 1: Recall abstract class syntax
In C#, the keyword abstract precedes class, 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 A
Quick Check:
abstract class syntax = "abstract class" [OK]
Hint: abstract class keyword order: 'abstract class' [OK]
Common Mistakes:
Swapping 'class abstract' instead of 'abstract class'
Using interface syntax for abstract class
Missing 'abstract' keyword before method
3. Consider the following code snippet:
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?
medium
A. Breathing
Woof
B. Woof
Breathing
C. Breathing
D. Compilation error due to multiple inheritance
Solution
Step 1: Understand class and interface usage
The class Dog inherits from abstract class Mammal and implements interface IAnimal. It overrides Speak() and inherits Breathe().
Step 2: Trace method calls
Calling dog.Breathe() prints "Breathing". Calling dog.Speak() prints "Woof" as overridden in Dog.
Final Answer:
Breathing
Woof -> Option A
Quick Check:
Abstract class method + override = correct output [OK]
Hint: Abstract class methods run normally; override abstract methods [OK]
Common Mistakes:
Thinking multiple inheritance causes error in C#
Confusing order of output lines
Missing override keyword causing compile error
4. The following code causes a compilation error. What is the main reason?
abstract class Shape {
public abstract void Draw();
}
class Circle : Shape {
public void Draw() {
Console.WriteLine("Drawing Circle");
}
}
medium
A. Draw() method must be static in Circle.
B. Abstract classes cannot have abstract methods.
C. Circle cannot inherit from Shape because Shape is abstract.
D. Circle must declare Draw() as override, not just public.
When a class inherits an abstract method, it must override it using the override keyword.
Step 2: Check Circle class method
The Draw() method in Circle is declared as public void Draw() but missing override, causing a compile error.
Final Answer:
Circle must declare Draw() as override, not just public. -> Option D
Quick Check:
Override abstract method = must use 'override' keyword [OK]
Hint: Override abstract methods with 'override' keyword [OK]
Common Mistakes:
Omitting 'override' keyword on abstract method implementation
Thinking abstract classes can't have abstract methods
Assuming static needed for overridden methods
5. You need to design a system where multiple unrelated classes must implement a method Log() but also share some common logging code. Which approach is best in C#?
hard
A. Use only an interface ILogger with Log() method and no shared code.
B. Use only an abstract class with Log() as abstract method and shared code implemented.
C. Create an interface ILogger with Log() and a separate abstract class with shared code, then have classes implement both.
D. Use a concrete class with Log() and inherit it in all classes.
Solution
Step 1: Analyze requirements
Multiple unrelated classes must implement Log() 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 C
Quick Check:
Interface + abstract class = behavior + shared code [OK]
Hint: Combine interface for behavior and abstract class for shared code [OK]
Common Mistakes:
Trying to use only abstract class for unrelated classes
Ignoring shared code needs
Assuming multiple inheritance of classes is allowed