Inheritance is one of the fundamental attributes of object-oriented
programming. It allows you to define a child class that reuses (inherits),
extends, or modifies the behavior of a parent class. Inheritance is a feature of
object-oriented programming languages that allows you to define a base class
that provides specific functionality (data and behavior) and to define derived
classes that either inherit or override that functionality, so Inheritance is
one of the primary concepts of object-oriented programming. It allows you to
reuse existing code.
Example of Inheritance
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InheritanceExampleNamespace
{
public class ParentClass
{
string parentString;
public ParentClass()
{
Console.WriteLine("i am
Parent Constructor.");
}
public ParentClass(string myString)
{
parentString = myString;
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a
Parent Class method .");
}
}
public class ChildClass : ParentClass
{
public ChildClass() : base("From
Derived")
{
Console.WriteLine(" i am
Child Constructor.");
}
public new void print()
{
base.print();
Console.WriteLine("I'm a
Child Class method .");
}
public static void Main()
{
ChildClass objchild = new ChildClass();
objchild.print();
((ParentClass)objchild).print();
Console.ReadLine();
}
}
}
|
Types of inheritance in
C#.
1. Single Inheritance
2. Hierarchical Inheritance
3. Multi-Level Inheritance
4. Multiple (using Interface) Inheritance
Syntax of Single Inheritance
Example of Single Inheritance:-
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InheritanceExampleNamespace
{
//single Inheritance
public class A
{
public void welcome()
{
Console.WriteLine("welcome to
Inheritance world , i am inside class A");
}
}
public class B : A //class B is derived by class A
{
public void Hello()
{
Console.WriteLine("inside
Hello function of class B");
}
}
public class singleInheritanceExampleClass
{
public static void Main()
{
B obj = new B(); //create obj of child class
obj.welcome(); // call the
function of Class A (base class)
obj.Hello(); // call the
function of child class (drived class)
Console.ReadLine();
}
}
}
|
Hierarchical
inheritance
In hierarchical inheritance there are multiple classes
derived from one base class.
Example of Hierarchical
inheritance
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InheritanceExampleNamespace
{
//Hierarchical
Inheritance
public class A
{
public void welcome()
{
Console.WriteLine("welcome to
Inheritance world , i am inside class A");
}
}
public class B : A //class B is derived by class A
{
public void Hello()
{
Console.WriteLine("inside
Hello function of class B");
}
}
public class C : A //class C is derived by class A
{
public void ClassC_Hello()
{
Console.WriteLine("inside
ClassC_Hello function of class C");
}
}
public class singleInheritanceExampleClass
{
public static void Main()
{
C obj = new C(); //create obj of class C
obj.welcome(); // call the
function of Class A (base class)
obj.ClassC_Hello();
B objB = new B(); //create obj of class B
objB.welcome();// call the
function of Class A (base class)
objB.Hello(); // call the function
of B class
Console.ReadLine();
}
}
}
|
Multilevel inheritance
In Multilevel inheritance one class is derived from another
derived class.
Example of Multilevel inheritance:-
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InheritanceExampleNamespace
{
//MultiLevel Inheritance
public class A
{
public void welcome()
{
Console.WriteLine("welcome to
Inheritance world , i am inside class A");
}
}
public class B : A //class B is derived by class A
{
public void Hello()
{
Console.WriteLine("inside
Hello function of class B");
}
}
public class C : B //class C is derived by class B
{
public void ClassC_Hello()
{
Console.WriteLine("inside
ClassC_Hello function of class C");
}
}
public class singleInheritanceExampleClass
{
public static void Main()
{
C obj = new C(); //create obj of child class
obj.welcome(); // call the
function of Class A (base class)
obj.Hello(); // call the
function of child class (parent class)
obj.ClassC_Hello();
Console.ReadLine();
}
}
}
|