Single Inheritance in C#
In this article we are going to see, Inheritance in C#.So, What is an Inheritance in C# ? An Inheritance is a concept or approach where one type can derived from other type. Just like a son gets his DNA from his parents. In c#, we can derive a type from other type. So the child type can inherit its parent type members. ( Members mean Methods,Property etc.)Now , we will see Single Inheritance where a class and inherit only one class. A class can not be derived from more than one class that’s why we called it as Single Inheritance. (Type means Classes,Interfaces and Abstract classes).Following is a simple program to understand it.
Code Snippet:-
/// <summary>
/// Abstract class
/// </summary>
public abstract class AbsClass
{
/// <summary>
/// Abstract method declaration
/// </summary>
/// <param name="_input"></param>
public abstract void WhatUgot(int _input);
}
/// <summary>
/// Non Abstract Class derived from Abstract class
/// </summary>
public class NonAbsClass : AbsClass
{
///// <summary>
///// Overriden Mthod
///// </summary>
///// <param name="_input"></param>
public override void WhatUgot(int _input)
{
Console.WriteLine("This is Overriden Method");
}
}
public class SingleInheritance
{
public static void Main()
{
NonAbsClass obj = new NonAbsClass();
obj.WhatUgot(1);
Console.ReadLine();
}
}
In the above example, We have derived “NonAbsClass” from Abstract class “AbsClass” and inherited its Method. Means we overrode the Abstract method of Abstract Class “AbsClass” in Non-Abstract Class “NonAbsClass”. Point to remember is we can not derive a normal class from more than one Class or Abstract Class that’s why we called it Single Inheritance.
Comments
Post a Comment