avatarSukhpinder Singh | .Net Developer

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

5285

Abstract

ass="hljs-comment">// No specific properties or methods</span> }</pre></div><h2 id="11de">Advantages</h2><p id="98a5">One of the main advantages of hierarchical inheritance is that it allows for code reuse and organization. By creating a hierarchy of classes, you can ensure that common functionality is shared across multiple classes, making it easier to maintain and update the code in the future. Additionally, hierarchical inheritance allows for more flexibility and extensibility, as new classes can be added to the hierarchy at any time without affecting existing code.</p><p id="35d7">Another advantage of hierarchical inheritance is that it allows for better code readability and abstraction. By creating a hierarchy of classes, you can represent complex relationships between objects in a more intuitive and natural way. This makes the code easier to understand and modify, even for developers who are not familiar with the application.</p><h2 id="06c3">In terms of use cases</h2><p id="3a05">Hierarchical inheritance is especially useful in applications that deal with complex data structures or have many different types of objects that share common properties and methods. For instance, hierarchical inheritance is often used in game development, where different game objects may share similar properties and methods, but also have their own unique characteristics.</p><h2 id="7ebd">Use Case example</h2><p id="bf45">A common use case for hierarchical inheritance in C# is in the development of user interfaces, where there are many different types of controls that share common properties and functionality, but also have unique characteristics.</p><p id="1085">For instance, imagine that you are developing a Windows Forms application that contains various types of controls, such as buttons, labels, textboxes, and dropdown lists. All of these controls have common properties, such as size, location, font, and colour, and common methods, such as event handling and data binding.</p><p id="8208">However, each control also has its own specific properties and methods. For instance, a textbox has a Text property that holds the text entered by the user, while a dropdown list has a SelectedValue property that holds the selected item value.</p><p id="dc58">To create a hierarchy of controls, you could define a base class called Control, which contains the common properties and methods shared by all controls. Then, you could create derived classes for each specific type of control, such as Button, Label, TextBox, and DropDownList. Each derived class would inherit from the Control class, but also add its own specific properties and methods.</p><div id="12bb"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Control</span> { <span class="hljs-keyword">public</span> <span class="hljs-built_in">int</span> Width { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> <span class="hljs-built_in">int</span> Height { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> Font Font { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> Color BackColor { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> Color ForeColor { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</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">OnClick</span>()</span> { <span class="hljs-comment">// Default click event handling</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">OnDataBinding</span>()</span> { <span class="hljs-comment">// Default data binding event handling</span> } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Button</span> : <span class="hljs-title">Control</span> { <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> Text { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</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">OnClick</span>()</span> { <span class="hljs-comment">// Button-specific click event handling</span> } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Label</span> : <span class="hljs-title">Control</span> { <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> Text { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } } <span class="hljs-keyword">public</span> <span class="hlj

Options

s-keyword">class</span> <span class="hljs-title">TextBox</span> : <span class="hljs-title">Control</span> { <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> Text { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</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">OnDataBinding</span>()</span> { <span class="hljs-comment">// Textbox-specific data binding event handling</span> } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">DropDownList</span> : <span class="hljs-title">Control</span> { <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> SelectedValue { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } } }</pre></div><p id="c81e">Using hierarchical inheritance in this way allows for better code organization, code reuse, and extensibility. New types of controls can be added to the hierarchy at any time, without affecting the existing code. Additionally, the code becomes more intuitive and easier to understand, as the hierarchy accurately reflects the relationships between different types of controls.</p><h2 id="0813">Conclusion</h2><p id="25ae">Hierarchical inheritance is a powerful and flexible feature of C# that allows developers to create complex and extensible class hierarchies. By using hierarchical inheritance, developers can reuse code, create more organized and structured code hierarchies, and achieve greater flexibility and extensibility in their applications.</p><p id="4c20">With a solid understanding of hierarchical inheritance, you can take your C# development skills to the next level and create more efficient, maintainable, and scalable software.</p><h2 id="8b79">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="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="8a68">Follow me on</h2><p id="a2f3"><a href="https://medium.com/c-sharp-progarmming">C# Publication</a>, <a href="https://www.linkedin.com/in/sukhpinder-singh-532284a2/">LinkedIn</a>, <a href="https://www.instagram.com/sukhpindersukh/">Instagram</a>, <a href="https://twitter.com/sukhsukhpinder">Twitter</a>, <a href="https://dev.to/ssukhpinder">Dev.to</a></p><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#: Hierarchical Inheritance

In C#, Hierarchical Inheritance is a type of inheritance where a derived class can inherit from a single base class, but the derived class can also act as a base class for other classes. In other words, the inheritance tree branches out into multiple directions, forming a hierarchy of classes.

Photo by Thalia Tran 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 Hierarchical Inheritance in C#

Getting Started

To better understand hierarchical inheritance in C#, let’s consider an example. Imagine that you are building an application to manage employee data in a company. You could have a base class called Employee, which contains common properties and methods that all employees share, such as name, address, salary, and hire date. Then, you could create two derived classes, Manager and Clerk, which inherit from the Employee class.

public class Employee
{
    public string Name { get; set; }
    public string Address { get; set; }
    public decimal Salary { get; set; }
    public DateTime HireDate { get; set; }
}
public class Manager : Employee
{
    public string Department { get; set; }
}
public class Clerk : Employee
{
    public int TypingSpeed { get; set; }
}

In this example, the Manager and Clerk classes inherit all the properties and methods from the Employee class, while also adding their own specific properties. For instance, the Manager class has a Department property that indicates the department the manager works in, while the Clerk class has a TypingSpeed property that indicates the speed at which the clerk can type.

Now, let’s say that you want to create a new class called Salesperson that also inherits from the Employee class, but does not have any specific properties or methods of its own. In this case, you can use hierarchical inheritance to create a Salesperson class that inherits directly from the Employee class.

public class Salesperson : Employee
{
    // No specific properties or methods
}

Advantages

One of the main advantages of hierarchical inheritance is that it allows for code reuse and organization. By creating a hierarchy of classes, you can ensure that common functionality is shared across multiple classes, making it easier to maintain and update the code in the future. Additionally, hierarchical inheritance allows for more flexibility and extensibility, as new classes can be added to the hierarchy at any time without affecting existing code.

Another advantage of hierarchical inheritance is that it allows for better code readability and abstraction. By creating a hierarchy of classes, you can represent complex relationships between objects in a more intuitive and natural way. This makes the code easier to understand and modify, even for developers who are not familiar with the application.

In terms of use cases

Hierarchical inheritance is especially useful in applications that deal with complex data structures or have many different types of objects that share common properties and methods. For instance, hierarchical inheritance is often used in game development, where different game objects may share similar properties and methods, but also have their own unique characteristics.

Use Case example

A common use case for hierarchical inheritance in C# is in the development of user interfaces, where there are many different types of controls that share common properties and functionality, but also have unique characteristics.

For instance, imagine that you are developing a Windows Forms application that contains various types of controls, such as buttons, labels, textboxes, and dropdown lists. All of these controls have common properties, such as size, location, font, and colour, and common methods, such as event handling and data binding.

However, each control also has its own specific properties and methods. For instance, a textbox has a Text property that holds the text entered by the user, while a dropdown list has a SelectedValue property that holds the selected item value.

To create a hierarchy of controls, you could define a base class called Control, which contains the common properties and methods shared by all controls. Then, you could create derived classes for each specific type of control, such as Button, Label, TextBox, and DropDownList. Each derived class would inherit from the Control class, but also add its own specific properties and methods.

public class Control
{
    public int Width { get; set; }
    public int Height { get; set; }
    public Font Font { get; set; }
    public Color BackColor { get; set; }
    public Color ForeColor { get; set; }
    public virtual void OnClick()
        {
            // Default click event handling
        }
        public virtual void OnDataBinding()
        {
            // Default data binding event handling
        }
    }
    public class Button : Control
    {
        public string Text { get; set; }
        public override void OnClick()
        {
            // Button-specific click event handling
        }
    }
    public class Label : Control
    {
        public string Text { get; set; }
    }
    public class TextBox : Control
    {
        public string Text { get; set; }
        public override void OnDataBinding()
        {
            // Textbox-specific data binding event handling
        }
    }
    public class DropDownList : Control
    {
        public string SelectedValue { get; set; }
    }
}

Using hierarchical inheritance in this way allows for better code organization, code reuse, and extensibility. New types of controls can be added to the hierarchy at any time, without affecting the existing code. Additionally, the code becomes more intuitive and easier to understand, as the hierarchy accurately reflects the relationships between different types of controls.

Conclusion

Hierarchical inheritance is a powerful and flexible feature of C# that allows developers to create complex and extensible class hierarchies. By using hierarchical inheritance, developers can reuse code, create more organized and structured code hierarchies, and achieve greater flexibility and extensibility in their applications.

With a solid understanding of hierarchical inheritance, you can take your C# development skills to the next level and create more efficient, maintainable, and scalable software.

More on Inheritance

Follow me on

C# Publication, LinkedIn, Instagram, Twitter, Dev.to

Dotnet
Oops Concepts
Inheritance
Csharp
Programming
Recommended from ReadMedium