Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Derived Classes | OOP Essentials
C# Beyond Basics
course content

Course Content

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookDerived Classes

When we say a class is derived from another class, it means that it has all the fields and methods of the parent class and in addition to it, the derived class can contain additional fields and methods as well.

The syntax for making an inherited class is the following:

cs

index

copy
1234567891011
// Base class (parent class) public class BaseClass { // Fields and methods of the base class } // Derived class (child class) public class DerivedClass : BaseClass { // Additional fields and methods specific to the derived class }

Here's an example with some concrete code:

cs

index

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; // Base class (parent class) public class Animal { public string Name; public void Eat() { Console.WriteLine($"{Name} is eating."); } } // Derived class (child class) public class Dog : Animal { public void Bark() { Console.WriteLine($"{Name} is barking."); } } class ConsoleApp { static void Main() { // Creating an instance of the derived class Dog myDog = new Dog(); myDog.Name = "Buddy"; // Using inherited method from the base class myDog.Eat(); // Using method specific to the derived class myDog.Bark(); } }

In this example, Dog is the derived class, inheriting from the Animal base class. The Dog class has access to the Name property and the Eat method from the Animal class. Additionally, it introduces a new method, Bark, which is specific to the Dog class.

As illustrated in the diagram, there can be cases where a class inherits from a class which is already a class from some other:

cs

index

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; // Base class public class Animal { public void Eat() { Console.WriteLine("Animal is eating."); } } // Intermediate class inheriting from Animal public class Mammal : Animal { public void GiveBirth() { Console.WriteLine("Mammal is giving birth."); } } // Derived class inheriting from Mammal public class Dog : Mammal { public void Bark() { Console.WriteLine("Dog is barking."); } } class Program { static void Main() { Dog myDog = new Dog(); // Methods from the base class myDog.Eat(); // Methods from the intermediate class myDog.GiveBirth(); // Methods from the derived class myDog.Bark(); } }

In such a case, the class at the top-most level is called the Super Class. In this case Animal is the super class. Such a case where where there are multiple levels of inheritence is called Multi-Level Inheritence.

1. In C#, which keyword or symbol is used to declare a derived class?
2. In Multi-Level inheritance, what is the class at the top called?
3. Is Multilevel inheritance possible in C#?
In C#, which keyword or symbol is used to declare a derived class?

In C#, which keyword or symbol is used to declare a derived class?

Select the correct answer

In Multi-Level inheritance, what is the class at the top called?

In Multi-Level inheritance, what is the class at the top called?

Select the correct answer

Is Multilevel inheritance possible in C#?

Is Multilevel inheritance possible in C#?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt