0
0
C Sharp (C#)programming~5 mins

Interface vs abstract class decision in C Sharp (C#) - Quick Revision & Key Differences

Choose your learning style9 modes available
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
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
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
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
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
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.