avatarSukhpinder Singh | .Net Developer

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

4316

Abstract

rd">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">SetWidth</span>(<span class="hljs-params"><span class="hljs-built_in">int</span> w</span>)</span> { width = w; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">SetHeight</span>(<span class="hljs-params"><span class="hljs-built_in">int</span> h</span>)</span> { height = h; } <span class="hljs-keyword">protected</span> <span class="hljs-built_in">int</span> width; <span class="hljs-keyword">protected</span> <span class="hljs-built_in">int</span> height; } <span class="hljs-comment">// Derived class</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Rectangle</span> : <span class="hljs-title">Shape</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-built_in">int</span> <span class="hljs-title">GetArea</span>()</span> { <span class="hljs-keyword">return</span> (width * height); } } <span class="hljs-comment">// Main method</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-built_in">string</span>[] args</span>)</span> { Rectangle rect = <span class="hljs-keyword">new</span> Rectangle(); rect.SetWidth(<span class="hljs-number">10</span>); rect.SetHeight(<span class="hljs-number">20</span>); <span class="hljs-comment">// Print the area of the rectangle</span> Console.WriteLine(<span class="hljs-string">"Area of Rectangle: {0}"</span>, rect.GetArea()); Console.ReadKey(); } }</pre></div><p id="ac24">In this code, we have a base class <code>Shape</code>, which has two methods for setting the <code>width</code> and <code>height</code> of a shape. The <code>Rectangle</code> class derives from <code>Shape</code> and adds a method <code>GetArea()</code> to calculate the area of a rectangle. In the <code>Main</code> method, we create an object of <code>Rectangle</code>, set its <code>width</code> and <code>height</code>, and then call the <code>GetArea()</code> method to calculate the area of the rectangle.</p><p id="a019"><b>For another example, </b>consider the following code that defines a base class <code>Animal</code> and a derived class <code>Dog</code> that inherits from <code>Animal</code>:</p><div id="f843"><pre><span class="hljs-keyword">using</span> System; <span class="hljs-comment">// base class</span> <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">void</span> <span class="hljs-title">Eat</span>()</span> { Console.WriteLine(<span class="hljs-string">"The animal is eating."</span>); } } <span class="hljs-comment">// derived class</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">void</span> <span class="hljs-title">Bark</span>()</span> { Console.WriteLine(<span class="hljs-string">"The dog is barking."</span>); } } <span class="hljs-keyword">class</span> <span class="hljs-title">Program</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-built_in">string</span>[] args</span>)</span> { Dog dog = <span class="hljs-keyword">new</span> Dog(); dog.Eat(); dog.Bark(); } }</pre></div><p id="e4d2">The output of this program will be:</p><div id="7b80"><pre>The animal <span class="hljs-keyword">is</span> eating. The dog <span class="hljs-keyword">is</span> barking.</pre></div><p id="55d5">In this example, we create an object of the <code>Dog</code> class and call both the <code>Eat()</code> method inherited from the base class <code>Animal</code>, and the <code>B

Options

ark()</code> the method defined in the <code>Dog</code> class. The output is the result of the <code>WriteLine</code> statements in each of these methods.</p><h2 id="e2b0">Conclusion</h2><p id="1d4a">Inheritance is a powerful feature of C# that allows us to reuse code and create new classes that inherit properties and behaviours from existing classes. We can use inheritance to create a hierarchy of classes that share standard functionality and add new functionality where needed. Using inheritance, we can write more concise and maintainable code, which is essential for larger projects.</p><h2 id="b7cd">More on Inheritance</h2><div id="9af7" class="link-block"> <a href="https://readmedium.com/mastering-inheritance-in-c-single-inheritance-96560724de45"> <div> <div> <h2>Mastering Inheritance in C#: Single Inheritance</h2> <div><h3>In C#, inheritance is a powerful feature that allows developers to create new classes based on existing classes. With…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*FtnJrufxQny9Pfgo)"></div> </div> </div> </a> </div><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#: The Complete Guide

Inheritance is a fundamental concept in object-oriented programming (OOP) and plays a significant role in C#. It is a mechanism that allows a new class to be based on an existing class, inheriting its properties and behaviours and also adding new ones. This article will discuss inheritance in C# and how to use it in our code.

Photo by Chris Ried on Unsplash

Prerequisites

  • Any basic programming language knowledge.

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

Learning Objectives

  • What is inheritance in C#
  • What are different types of inheritance available in C#
  • How to implement inheritance in C#

Getting Started

Inheritance in C#

In C#, we can create a derived class from a base class using the syntax:

class DerivedClass : BaseClass
{
    // class members
}

In this syntax, DerivedClass is a new class that is made, and BaseClass is the class from which it is derived. The DerivedClass class will automatically inherit all the members of the BaseClass, including fields, methods, properties, and events.

For example, let’s say we have a Person class with fields for name and age. We can create a new class Student that derives from Person and adds a new field for grade.

class Person
{
    public string name;
    public int age;
}
class Student : Person
{
    public int grade;
}

In this example, the Student class inherits the name and age fields from the Person class and adds a new grade field.

Inheritance Types

There are five types of inheritance in C#:

  1. Single Inheritance: A derived class inherits from a single base class.
  2. Multi-level Inheritance: A derived class inherits from a base class, which itself inherits from another base class.
  3. Hierarchical Inheritance: Multiple classes inherit from a single base class.
  4. Multiple Inheritance: A derived class inherits from multiple base classes.
  5. Hybrid Inheritance: It is a combination of any two or more types of inheritance.

Code Example: Let’s consider an example to understand inheritance in C#.

using System;
class Shape
{
    public void SetWidth(int w)
    {
        width = w;
    }
    public void SetHeight(int h)
    {
        height = h;
    }
    protected int width;
    protected int height;
}
// Derived class
class Rectangle : Shape
{
    public int GetArea()
    {
        return (width * height);
    }
}
// Main method
class Program
{
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle();
        rect.SetWidth(10);
        rect.SetHeight(20);
        // Print the area of the rectangle
        Console.WriteLine("Area of Rectangle: {0}", rect.GetArea());
        Console.ReadKey();
    }
}

In this code, we have a base class Shape, which has two methods for setting the width and height of a shape. The Rectangle class derives from Shape and adds a method GetArea() to calculate the area of a rectangle. In the Main method, we create an object of Rectangle, set its width and height, and then call the GetArea() method to calculate the area of the rectangle.

For another example, consider the following code that defines a base class Animal and a derived class Dog that inherits from Animal:

using System;
// base class
class Animal
{
    public void Eat()
    {
        Console.WriteLine("The animal is eating.");
    }
}
// derived class
class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("The dog is barking.");
    }
}
class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog();
        dog.Eat();
        dog.Bark();
    }
}

The output of this program will be:

The animal is eating.
The dog is barking.

In this example, we create an object of the Dog class and call both the Eat() method inherited from the base class Animal, and the Bark() the method defined in the Dog class. The output is the result of the WriteLine statements in each of these methods.

Conclusion

Inheritance is a powerful feature of C# that allows us to reuse code and create new classes that inherit properties and behaviours from existing classes. We can use inheritance to create a hierarchy of classes that share standard functionality and add new functionality where needed. Using inheritance, we can write more concise and maintainable code, which is essential for larger projects.

More on Inheritance

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

Oops Concepts
Inheritance
Dotnet
Csharp
Dotnet Core
Recommended from ReadMedium