avatarSukhpinder Singh | .Net Developer

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4253

Abstract

ne a derived class <code>Dog</code> that inherits from <code>Animal</code>. The <code>Dog</code> class also defines a new method <code>Bark()</code>, which is not present in the <code>Animal</code> class.</p><h2 id="b888">Using Single Inheritance in C#</h2><p id="4940">To use single inheritance in C#, we create a new instance of the derived class and call its methods or properties. The derived class will automatically inherit all of the members of its base class, including any methods or properties defined in the base class. For example, consider the following code that creates an instance of the <code>Dog</code> class and calls its <code>Eat()</code> and <code>Bark()</code> methods:</p><div id="65c0"><pre><span class="hljs-type">Dog</span> <span class="hljs-variable">myDog</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Dog</span>(); myDog.Eat(); <span class="hljs-comment">// output: "The animal is eating."</span> myDog.Bark(); <span class="hljs-comment">// output: "The dog is barking."</span></pre></div><p id="5b9f">In this example, we create a new instance of the <code>Dog</code> class and call its <code>Eat()</code> and <code>Bark()</code> methods. The output of these methods is determined by the <code>WriteLine</code> statements in each method. Because <code>Dog</code> inherits from <code>Animal</code>, it also inherits the <code>Eat()</code> method defined in the <code>Animal</code> class.</p><h2 id="1cde">Overriding Base Class Members</h2><p id="9a97">In addition to inheriting members from the base class, a derived class can also override or modify the behaviour of a base class member. To do this, the derived class must define a method with the same name and signature as the base class member. This is known as method overriding.</p><p id="6edb">For example, consider the following modified code that adds a virtual method <code>MakeSound()</code> to the <code>Animal</code> class, which is then overridden in the <code>Dog</code> class:</p><div id="5a85"><pre><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">virtual</span> <span class="hljs-keyword">void</span> <span class="hljs-title">MakeSound</span>()</span> { Console.WriteLine(<span class="hljs-string">"The animal makes a sound."</span>); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Eat</span>()</span> { Console.WriteLine(<span class="hljs-string">"The animal is eating."</span>); } } <span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span> : <span class="hljs-title">Animal</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">MakeSound</span>()</span> { Console.WriteLine(<span class="hljs-string">"The dog barks."</span>); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Bark</span>()</span> { Console.WriteLine(<span class="hljs-string">"The dog is barking."</span>); } } }</pre></div><p id="6a65">In this example, we add a virtual method <code>MakeSound()</code>to the <code>Animal</code> class that prints a generic message indicating that the animal makes a sound. We then override this method in the <code>Dog</code> class to print a message showing that the dog barks instead. Note that we use the <code>override</code> keyword to show that the <code>MakeSound()</code> method in <code>Dog</code> is overriding the virtual <code>MakeSound()</code> method in <code>Animal</code>.</p><p id="4b8b">Now, when we call the <code>MakeSound()</code> method on an instance of the <code>Dog</code> class, we get the following output:</p><div id="d62e"><pre><span class="hljs-type">Dog</span> <span class="hljs-variable">myDog</span> <span class="hljs-operator">=</span> <span class="hljs-keywor

Options

d">new</span> <span class="hljs-title class_">Dog</span>(); myDog.MakeSound(); <span class="hljs-comment">// output: "The dog barks."</span></pre></div><p id="e908">In this case, the <code>MakeSound()</code> method defined in the <code>Dog</code> class overrides the virtual <code>MakeSound()</code> method defined in the <code>Animal</code> class. When we call <code>MakeSound()</code> on an instance of the <code>Dog</code> class, we get the message "The dog barks." instead of "The animal makes a sound.".</p><h2 id="66e2">Final Thoughts</h2><p id="1e97">Single inheritance is a powerful and widely used feature in object-oriented programming that allows developers to create new classes based on existing classes. By using single inheritance in C#, developers can easily extend the functionality of existing classes and create new classes tailored to their specific needs.</p><p id="9f9b">In this article, we have explored the concept of single inheritance in C#, including how to use it with examples. We have also seen how to override base class members in a derived class. With a solid understanding of single inheritance, you can begin to create more complex and powerful object-oriented programs in C#.</p><h2 id="bf5e">More on Inheritance</h2><div id="a8fd" class="link-block"> <a href="https://readmedium.com/mastering-inheritance-in-c-hybrid-inheritance-842f55216010"> <div> <div> <h2>Mastering Inheritance in C#: Hybrid Inheritance</h2> <div><h3>Inheritance is a powerful feature in object-oriented programming that allows classes to inherit properties and methods…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Y8x3P2hZ13_JFkPu)"></div> </div> </div> </a> </div><div id="c8e0" class="link-block"> <a href="https://readmedium.com/mastering-inheritance-in-c-multiple-inheritance-d12f76b89ccf"> <div> <div> <h2>Mastering Inheritance in C#: Multiple Inheritance</h2> <div><h3>Multiple inheritance is a concept in object-oriented programming where a class can inherit properties and methods from…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*jRT033FvnBZ-m9jd)"></div> </div> </div> </a> </div><div id="e2e4" class="link-block"> <a href="https://readmedium.com/mastering-inheritance-in-c-hierarchical-inheritance-74de4d9ecee1"> <div> <div> <h2>Mastering Inheritance in C#: Hierarchical Inheritance</h2> <div><h3>In C#, Hierarchical Inheritance is a type of inheritance where a derived class can inherit from a single base class…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*LnN-_dqUU31KLFFB)"></div> </div> </div> </a> </div><div id="ad0d" class="link-block"> <a href="https://readmedium.com/mastering-inheritance-in-c-multi-level-inheritance-3993e8ed1833"> <div> <div> <h2>Mastering Inheritance in C#: Multi-level Inheritance</h2> <div><h3>Inheritance is one of the key features of object-oriented programming that allows developers to create new classes…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*H90IT7BzBGTJwBsU)"></div> </div> </div> </a> </div><h2 id="a2f3">Follow me onC# Publication, LinkedIn, Instagram, Twitter, Dev.to</h2><figure id="01a2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Dpw8-hNGI2fDmosV4E8DVQ.png"><figcaption></figcaption></figure></article></body>

OOPS

Mastering Inheritance in C#: Single Inheritance

In C#, inheritance is a powerful feature that allows developers to create new classes based on existing classes. With inheritance, a new class, called a derived class or subclass, can inherit the properties and behaviours of an existing class, called a base class or superclass. Single inheritance is a type in which a derived class inherits from a single base class. In this article, we will explore the concept of single inheritance in C# in more depth, including how to use it with examples.

Photo by Matthew Henry on Unsplash

Prerequisites

  • Any basic programming language knowledge.

The article demonstrates inheritance using the C# programming language. So, to begin with, C#

Learning Objectives

  • How to implement single inheritance in C#

Getting Started

Single Inheritance in C#

Single inheritance is a simple way to create a new class based on an existing class. In single inheritance, a derived class inherits all of the members of a single base class, including its methods, properties, and fields. The derived class can also define new members, such as additional methods or properties, that are not present in the base class. This allows developers to create new classes similar to existing classes but with other functionality or modifications.

In C#, single inheritance is implemented using the colon (:) symbol to indicate that a class is derived from another class. For example, consider the following code that defines a base class Animal and a derived class Dog:

class Animal
{
    public void Eat()
    {
        Console.WriteLine("The animal is eating.");
    }
}
class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("The dog is barking.");
    }
}

In this example, we define a base class Animal with a single method Eat(). We then define a derived class Dog that inherits from Animal. The Dog class also defines a new method Bark(), which is not present in the Animal class.

Using Single Inheritance in C#

To use single inheritance in C#, we create a new instance of the derived class and call its methods or properties. The derived class will automatically inherit all of the members of its base class, including any methods or properties defined in the base class. For example, consider the following code that creates an instance of the Dog class and calls its Eat() and Bark() methods:

Dog myDog = new Dog();
myDog.Eat();  // output: "The animal is eating."
myDog.Bark(); // output: "The dog is barking."

In this example, we create a new instance of the Dog class and call its Eat() and Bark() methods. The output of these methods is determined by the WriteLine statements in each method. Because Dog inherits from Animal, it also inherits the Eat() method defined in the Animal class.

Overriding Base Class Members

In addition to inheriting members from the base class, a derived class can also override or modify the behaviour of a base class member. To do this, the derived class must define a method with the same name and signature as the base class member. This is known as method overriding.

For example, consider the following modified code that adds a virtual method MakeSound() to the Animal class, which is then overridden in the Dog class:

class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound.");
    }
    public void Eat()
        {
            Console.WriteLine("The animal is eating.");
        }
    }
    class Dog : Animal
    {
        public override void MakeSound()
        {
            Console.WriteLine("The dog barks.");
        }
        public void Bark()
        {
            Console.WriteLine("The dog is barking.");
        }
    }
}

In this example, we add a virtual method MakeSound()to the Animal class that prints a generic message indicating that the animal makes a sound. We then override this method in the Dog class to print a message showing that the dog barks instead. Note that we use the override keyword to show that the MakeSound() method in Dog is overriding the virtual MakeSound() method in Animal.

Now, when we call the MakeSound() method on an instance of the Dog class, we get the following output:

Dog myDog = new Dog();
myDog.MakeSound(); // output: "The dog barks."

In this case, the MakeSound() method defined in the Dog class overrides the virtual MakeSound() method defined in the Animal class. When we call MakeSound() on an instance of the Dog class, we get the message "The dog barks." instead of "The animal makes a sound.".

Final Thoughts

Single inheritance is a powerful and widely used feature in object-oriented programming that allows developers to create new classes based on existing classes. By using single inheritance in C#, developers can easily extend the functionality of existing classes and create new classes tailored to their specific needs.

In this article, we have explored the concept of single inheritance in C#, including how to use it with examples. We have also seen how to override base class members in a derived class. With a solid understanding of single inheritance, you can begin to create more complex and powerful object-oriented programs in C#.

More on Inheritance

Follow me onC# Publication, LinkedIn, Instagram, Twitter, Dev.to

Oops Concepts
Dotnet
Csharp
Dotnet Core
Code
Recommended from ReadMedium