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

Why inheritance is needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use inheritance in C#?

Which of the following best explains why inheritance is used in C# programming?

ATo allow one class to reuse code from another class, reducing duplication and improving maintainability.
BTo make all classes run faster by combining their code into one big class.
CTo prevent any class from having methods or properties, making the program simpler.
DTo force every class to have the same methods, even if they do different things.
Attempts:
2 left
💡 Hint

Think about how inheritance helps programmers avoid rewriting the same code multiple times.

Predict Output
intermediate
2:00remaining
Output of inheritance example

What is the output of this C# code?

C Sharp (C#)
class Animal {
    public void Speak() {
        System.Console.WriteLine("Animal speaks");
    }
}
class Dog : Animal {
    public void Bark() {
        System.Console.WriteLine("Dog barks");
    }
}
class Program {
    static void Main() {
        Dog d = new Dog();
        d.Speak();
        d.Bark();
    }
}
AAnimal speaks
BDog barks\nAnimal speaks
CAnimal speaks\nDog barks
DDog barks
Attempts:
2 left
💡 Hint

Remember that Dog inherits methods from Animal and can use both Speak and Bark.

🔧 Debug
advanced
2:00remaining
Why does this inheritance code cause an error?

Look at this C# code. Why does it cause a compile-time error?

C Sharp (C#)
class Vehicle {
    public void Move() {
        System.Console.WriteLine("Vehicle moves");
    }
}
class Car : Vehicle {
    public void Move(int speed) {
        System.Console.WriteLine($"Car moves at {speed} km/h");
    }
}
class Program {
    static void Main() {
        Car c = new Car();
        c.Move();
    }
}
AThe code is correct and will print "Car moves at 0 km/h".
BThe class Vehicle cannot be inherited because it has a method named Move.
CThe method Move in Car must have the same return type as in Vehicle.
DThe call c.Move() is ambiguous because Car has a Move method with a parameter, hiding Vehicle's Move method without parameters.
Attempts:
2 left
💡 Hint

Think about method hiding and how method signatures affect which method is called.

📝 Syntax
advanced
2:00remaining
Correct syntax for inheritance in C#

Which of the following code snippets correctly shows how to inherit class Base in class Derived in C#?

Aclass Derived : Base { }
Bclass Derived inherits Base { }
Cclass Derived extends Base { }
Dclass Derived -> Base { }
Attempts:
2 left
💡 Hint

Remember the symbol used in C# to indicate inheritance.

🚀 Application
expert
3:00remaining
How inheritance improves code maintenance

You have two classes, Car and Bike, both with methods to start and stop. How does using inheritance help if you want to add a new method Refuel to both?

ABy making Car inherit from Bike so it automatically gets Refuel.
BBy creating a base class with start, stop, and refuel methods, both Car and Bike can inherit it, so you add Refuel only once.
CBy deleting the start and stop methods and only keeping Refuel in both classes.
DBy copying the Refuel method code into both Car and Bike classes separately.
Attempts:
2 left
💡 Hint

Think about how inheritance lets you write shared code once and reuse it.