Inheritance helps us reuse code and organize related things easily. It lets one class get features from another, so we don't repeat ourselves.
When you have many things that share common features, like different types of animals.
When you want to add new features to an existing class without changing it.
When you want to group similar classes under one main class for easier management.
When you want to write code that works with different but related objects in the same way.
When you want to improve or extend the behavior of a class without rewriting it.
Syntax
C Sharp (C#)
class BaseClass {
// common features
}
class DerivedClass : BaseClass {
// extra features
}
The DerivedClass inherits all features from BaseClass.
Use : to show inheritance in C#.
Examples
Dog inherits from Animal, so Dog can Eat and Bark.
C Sharp (C#)
class Animal {
publicvoid Eat() {
Console.WriteLine("Eating food");
}
}
class Dog : Animal {
publicvoid Bark() {
Console.WriteLine("Barking");
}
}
Car inherits Start from Vehicle and adds Honk.
C Sharp (C#)
class Vehicle {
publicvoid Start() {
Console.WriteLine("Starting vehicle");
}
}
class Car : Vehicle {
publicvoid Honk() {
Console.WriteLine("Honking horn");
}
}
Sample Program
This program shows how Dog inherits Eat() from Animal and also has its own Bark() method.
C Sharp (C#)
using System;
class Animal {
publicvoid Eat() {
Console.WriteLine("Eating food");
}
}
class Dog : Animal {
publicvoid Bark() {
Console.WriteLine("Barking");
}
}
class Program {
staticvoid Main() {
Dog myDog = new Dog();
myDog.Eat(); // inherited method
myDog.Bark(); // own method
}
}
OutputSuccess
Important Notes
Inheritance helps avoid repeating the same code in multiple classes.
Derived classes can use and add to the features of base classes.
Use inheritance carefully to keep code simple and clear.
Summary
Inheritance lets one class get features from another to reuse code.
It helps organize related classes and add new features easily.
Use inheritance to write cleaner and more maintainable code.
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]
Common Mistakes:
Copy-pasting code instead of inheriting
Reversing inheritance direction
Not using inheritance to reuse code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.