How should you design this using abstract classes and methods in C#?
hard🚀 Application Q8 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
You are designing a system where multiple device types must implement their own way to connect to a network, but all devices share a method to check connection status. How should you design this using abstract classes and methods in C#?
ACreate a concrete base class with both Connect() and CheckStatus() methods implemented.
BCreate an interface with both Connect() and CheckStatus() methods.
CCreate an abstract class with an abstract Connect() method and a concrete CheckStatus() method.
DCreate an abstract class with both Connect() and CheckStatus() as abstract methods.
Step-by-Step Solution
Solution:
Step 1: Analyze requirements
Each device must implement Connect() differently, so Connect() should be abstract. CheckStatus() is shared and can have a common implementation.
Step 2: Evaluate options
Create an abstract class with an abstract Connect() method and a concrete CheckStatus() method. fits perfectly: abstract Connect() method and concrete CheckStatus() method in an abstract class. Create an interface with both Connect() and CheckStatus() methods. uses interface which cannot have concrete methods (before C# 8). Create a concrete base class with both Connect() and CheckStatus() methods implemented. does not allow different Connect() implementations. Create an abstract class with both Connect() and CheckStatus() as abstract methods. forces all methods to be abstract, so no shared implementation.
Final Answer:
Create an abstract class with an abstract Connect() method and a concrete CheckStatus() method. -> Option C
Quick Check:
Abstract method for varying behavior, concrete for shared logic [OK]
Quick Trick:Abstract for varying, concrete for shared methods [OK]
Common Mistakes:
MISTAKES
Making all methods abstract losing shared logic
Using interface when concrete method needed
Implementing all methods in base class losing polymorphism
Master "Polymorphism and Abstract Classes" in C Sharp (C#)
9 interactive learning modes - each teaches the same concept differently