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 inheritance in C#?
Inheritance is a way to create a new class from an existing class. The new class gets all the features of the existing class and can add new features or change existing ones.
Click to reveal answer
beginner
Why do we need inheritance?
Inheritance helps us reuse code, avoid repetition, and organize related classes in a clear way. It makes programs easier to maintain and extend.
Click to reveal answer
beginner
How does inheritance help with code reuse?
By inheriting from a base class, a new class automatically gets all the base class's methods and properties, so we don't have to write the same code again.
Click to reveal answer
beginner
What is a real-life example of inheritance?
Think of a car as a base class. A sports car and a family car can inherit from it. Both share common features like wheels and engine but have their own special features.
Click to reveal answer
intermediate
How does inheritance improve program maintenance?
When a change is needed in shared features, you only update the base class. All classes that inherit from it get the update automatically, saving time and reducing errors.
Click to reveal answer
What is the main purpose of inheritance in C#?
ATo reuse code and create a relationship between classes
BTo hide data from other classes
CTo make programs run faster
DTo create unrelated classes
✗ Incorrect
Inheritance allows a new class to reuse code from an existing class and establish a parent-child relationship.
Which of the following is true about inheritance?
AInheritance is only used for interfaces
BA derived class can access all private members of the base class
CInheritance makes code harder to maintain
DInheritance helps avoid code duplication
✗ Incorrect
Inheritance helps avoid writing the same code multiple times by reusing base class code.
If class B inherits from class A, what does class B get?
AOnly the methods of class A
BAll accessible members of class A
CNothing from class A
DOnly the properties of class A
✗ Incorrect
Class B inherits all accessible members (methods, properties, etc.) from class A.
Which real-life example best illustrates inheritance?
AA dog and a cat both being animals
BA car and a bicycle being the same
CA phone and a laptop being unrelated
DA tree and a flower being the same
✗ Incorrect
A dog and a cat both inherit common features from the animal class, showing inheritance.
How does inheritance help with program maintenance?
ABy duplicating code in many places
BBy making code more complex
CBy centralizing shared code in one place
DBy removing all comments
✗ Incorrect
Inheritance centralizes shared code in the base class, making updates easier and reducing errors.
Explain why inheritance is useful in programming with an example.
Think about how a child class can get features from a parent class.
You got /3 concepts.
Describe how inheritance can make maintaining code easier.
Consider what happens when you change something in the base class.
You got /3 concepts.
Practice
(1/5)
1. Why do we use inheritance in C# programming?
easy
A. To make programs run faster by skipping code
B. To create unrelated classes with no shared features
C. To reuse code from an existing class in a new class
D. To avoid writing any methods in classes
Solution
Step 1: Understand inheritance purpose
Inheritance allows a new class to get properties and methods from an existing class, so we don't rewrite code.
Step 2: Compare options
Only To reuse code from an existing class in a new class correctly describes code reuse through inheritance. Others describe unrelated or incorrect uses.
Final Answer:
To reuse code from an existing class in a new class -> Option C
Quick Check:
Inheritance = Code reuse [OK]
Hint: Inheritance means new class gets old class features [OK]
Common Mistakes:
Thinking inheritance makes code run faster
Believing inheritance creates unrelated classes
Assuming inheritance removes need for methods
2. Which of the following is the correct syntax to inherit class Animal in C#?
easy
A. class Dog inherits Animal { }
B. class Dog : Animal { }
C. class Dog extends Animal { }
D. class Dog -> Animal { }
Solution
Step 1: Recall C# inheritance syntax
In C#, a class inherits another using a colon (:), like class Child : Parent { }.
Step 2: Check each option
class Dog : Animal { } uses the correct colon syntax. The other options use incorrect keywords or symbols.
Final Answer:
class Dog : Animal { } -> Option B
Quick Check:
Inheritance syntax in C# uses ':' [OK]
Hint: Use ':' to inherit a class in C# [OK]
Common Mistakes:
Using 'inherits' instead of ':'
Using 'extends' like in Java
Using arrows or other symbols
3. What will be the output of this C# code?
class Animal { public void Speak() { Console.WriteLine("Animal speaks"); } }
class Dog : Animal { public void Bark() { Console.WriteLine("Dog barks"); } }
var d = new Dog();
d.Speak();
medium
A. Dog barks
B. No output
C. Compile error
D. Animal speaks
Solution
Step 1: Understand inheritance and method calls
Dog inherits Animal, so Dog objects can call Animal's methods like Speak().
Hint: Inherited methods can be called on child objects [OK]
Common Mistakes:
Thinking Bark() runs instead of Speak()
Expecting compile error due to inheritance
Assuming no output without calling Bark()
4. Identify the error in this inheritance code:
class Vehicle { public void Move() { Console.WriteLine("Moving"); } }
class Car Vehicle { public void Honk() { Console.WriteLine("Honk!"); } }
medium
A. Missing colon ':' between Car and Vehicle
B. Method Move() should be abstract
C. Car class cannot have methods
D. Vehicle class must be sealed
Solution
Step 1: Check inheritance syntax
In C#, inheritance requires a colon ':' between child and parent class names.
Step 2: Locate the syntax error
The code uses 'class Car Vehicle' missing the colon, causing a syntax error.
Final Answer:
Missing colon ':' between Car and Vehicle -> Option A
Quick Check:
Inheritance needs ':' separator [OK]
Hint: Remember ':' after child class name for inheritance [OK]
Common Mistakes:
Omitting ':' in inheritance
Thinking methods must be abstract
Believing parent class must be sealed
5. You want to create a class ElectricCar that has all features of Car plus a new method ChargeBattery(). Which is the best way to do this using inheritance?
hard
A. Make ElectricCar inherit Car and add ChargeBattery() method
B. Copy all Car code into ElectricCar and add ChargeBattery()
C. Make Car inherit ElectricCar and add ChargeBattery()
D. Create ElectricCar without inheriting Car and add ChargeBattery()
Solution
Step 1: Understand inheritance for extending features
Inheritance lets ElectricCar reuse Car's features and add new ones like ChargeBattery().
Step 2: Evaluate options for best practice
Make ElectricCar inherit Car and add ChargeBattery() method correctly uses inheritance to extend Car. Copying code duplicates work. Making Car inherit ElectricCar reverses the logic. Not inheriting loses reuse.
Final Answer:
Make ElectricCar inherit Car and add ChargeBattery() method -> Option A
Quick Check:
Extend with inheritance, add new methods [OK]
Hint: Extend existing class, add new methods in child [OK]