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

Abstract classes and methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an abstract class in C#?
An abstract class is a class that cannot be instantiated directly. It is designed to be a base class for other classes and can contain abstract methods that must be implemented by derived classes.
Click to reveal answer
beginner
What is an abstract method?
An abstract method is a method declared in an abstract class without an implementation. Derived classes must provide their own implementation of this method.
Click to reveal answer
beginner
Can you create an object of an abstract class?
No, you cannot create an object of an abstract class directly. You must create an object of a derived class that implements all abstract methods.
Click to reveal answer
intermediate
Why use abstract classes and methods?
They help define a common template for related classes, ensuring that certain methods are implemented in all derived classes, promoting code consistency and reuse.
Click to reveal answer
beginner
Show a simple example of an abstract class with an abstract method in C#.
abstract class Animal {
    public abstract void MakeSound();
}

class Dog : Animal {
    public override void MakeSound() {
        System.Console.WriteLine("Woof!");
    }
}
Click to reveal answer
Which keyword is used to declare an abstract class in C#?
Aabstract
Bvirtual
Coverride
Dsealed
Can an abstract method have a body (implementation) in C#?
AOnly if the class is sealed
BYes, always
CNo, never
DOnly if marked virtual
What must a derived class do with an abstract method?
AOverride and implement it
BIgnore it
CDelete it
DCall base method
Can you instantiate an abstract class directly?
AYes
BOnly with new keyword
COnly if it has no abstract methods
DNo
Which of these is true about abstract classes?
AThey cannot inherit from other classes
BThey can contain both abstract and non-abstract methods
CThey cannot have constructors
DThey can only contain abstract methods
Explain what an abstract class and an abstract method are, and why they are useful.
Think about how you create a blueprint for other classes.
You got /4 concepts.
    Write a simple C# example showing an abstract class with one abstract method and a derived class implementing that method.
    Use Animal and Dog as example classes.
    You got /4 concepts.