- Back to Home »
- C#.NET »
- C#.NET Inheritance - Single, Multi level, multiple, hierarchical, Hybrid
Posted by :
Sudhir Chekuri
Tuesday, 5 January 2016
Acquiring or getting the properties from one class to another class is known as Inheritance
In Object Oriented approach if we can apply parent/child relation between classes.
Members of the parent class can be consumed by the child classes.
Output
using System;
Output for C#.NET single inheritance
car
bike
car
When a derived class is created from another derived class, then that inheritance is called as Multi level Inheritance
Syntax for C#.NET multi level inheritance
class parent
{
body
}
class child : parent
{
body
}
class subchild : child
{
body
}
Example for C#.NET multi level inheritance
Program
C#.NET Hierarchical Inheritance
Output for C# hierarchical inheritance
helicopter
helicopter
car
helicopter
bike
Multiple Inheritance can contain only one derived class, two or more base classes.
When a derived class is created from more than one base class then that inheritance is called as Multiple Inheritance
Multiple Inheritance is not supported in C#.NET because of ambiguity or confusion problem.
Syntax for C#.NET Multiple inheritance
class Parent
{
body
}
class parent2
{
body
}
class child : parent,parent2
Example for C#.NET Mulitple inheritance with error
Program
Output
An error will occur because a class cannot have multiple base classes
In Object Oriented approach if we can apply parent/child relation between classes.
Members of the parent class can be consumed by the child classes.
Syntax for C#.NET Inheritance
class parent
{
body
}
class child : parent
{
body
}
Example program for C#.NET Single Inheritance with 2 classes a & b with 2 methods display & show inside each them
Program
class parent
{
body
}
class child : parent
{
body
}
Example program for C#.NET Single Inheritance with 2 classes a & b with 2 methods display & show inside each them
Program
using System;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
b o1 = new b();//creating object for the derived class
o1.show();//calling
method from child class
o1.display();//calling
method from parent class
Console.ReadKey();
}
class a//parent
or base class
{
public void display()
{
Console.WriteLine("display");
}
}
class b : a//child or derived class inherited with base class
{
public void show()
{
Console.WriteLine("show");
}
}
}
}
show
display
display
C#.NET Single Inheritance
When a single derived class is derived from single base class, then that inheritance is called as single inheritance
In single inheritance we have single base class that is inherited by the derived class.
Here the derived class has all the features of the base class and can add some new features.
The inheritance also depends on the access specifier that is used at the time of base class inheritance.
Syntax for C#.NET single Inheritance
class Parent
{
body
}
class child : parent
{
body
}
Example for C#.NET single inheritance
Program
In single inheritance we have single base class that is inherited by the derived class.
Here the derived class has all the features of the base class and can add some new features.
The inheritance also depends on the access specifier that is used at the time of base class inheritance.
Syntax for C#.NET single Inheritance
class Parent
{
body
}
class child : parent
{
body
}
Example for C#.NET single inheritance
Program
using System;
namespace SingleInheritance
{
class Program
{
static void Main(string[] args)
{
dad d = new dad();
d.car();
son s = new son();
s.bike();
s.car();
Console.ReadKey();
}
class dad
{
public void car()
{
Console.WriteLine("car");
}
}
class son : dad
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}
Output for C#.NET single inheritance
car
bike
car
C#.NET Multi level Inheritance
Acquiring properties to a sub child class from the child class and also from the parent class.When a derived class is created from another derived class, then that inheritance is called as Multi level Inheritance
Syntax for C#.NET multi level inheritance
class parent
{
body
}
class child : parent
{
body
}
class subchild : child
{
body
}
Example for C#.NET multi level inheritance
Program
using System;
namespace MultiLevelInheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g = new grandfather();
g.helicopter();
dad d = new dad();
d.car();
d.helicopter();
son s = new son();
s.bike();
s.car();
s.helicopter();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad : grandfather
{
public void car()
{
Console.WriteLine("car");
}
}
class son : dad
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}
Output
for C#.NET Multi level inheritance example
helicopter
car
helicopter
bike
car
helicopter
helicopter
car
helicopter
bike
car
helicopter
C#.NET Hierarchical Inheritance
Single parent class with multiple child classes are called as Hierarchical Inheritance.
In other words when more than one derived class are created from single base class is called Hierarchical Inheritance.
Syntax for C#.NET hierarchical inheritance
class parent
{
body
}
class child1 : parent
{
body
}
class child2 : parent
{
body
}
Example for C#.NET hierarchical inheritance
Program
In other words when more than one derived class are created from single base class is called Hierarchical Inheritance.
Syntax for C#.NET hierarchical inheritance
class parent
{
body
}
class child1 : parent
{
body
}
class child2 : parent
{
body
}
Example for C#.NET hierarchical inheritance
Program
using System;
namespace HierarchicalInheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g = new grandfather();
g.helicopter();
dad d = new dad();
d.helicopter();
d.car();
son s = new son();
s.helicopter();
s.bike();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad : grandfather
{
public void car()
{
Console.WriteLine("car");
}
}
class son : grandfather
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}
Output for C# hierarchical inheritance
helicopter
helicopter
car
helicopter
bike
C#.NET Multiple Inheritance
One child with two parents is known as Multiple Inheritance.Multiple Inheritance can contain only one derived class, two or more base classes.
When a derived class is created from more than one base class then that inheritance is called as Multiple Inheritance
Multiple Inheritance is not supported in C#.NET because of ambiguity or confusion problem.
Syntax for C#.NET Multiple inheritance
class Parent
{
body
}
class parent2
{
body
}
class child : parent,parent2
Example for C#.NET Mulitple inheritance with error
Program
using System;
namespace MultipleInheritance
{
class Program
{
static void Main(string[] args)
{
grandfather g = new grandfather();
g.helicopter();
dad d = new dad();
d.car();
Console.ReadKey();
}
class grandfather
{
public void helicopter()
{
Console.WriteLine("helicopter");
}
}
class dad
{
public void car()
{
Console.WriteLine("car");
}
}
class son : dad, grandfather
{
public void bike()
{
Console.WriteLine("bike");
}
}
}
}
Output
An error will occur because a class cannot have multiple base classes
This comment has been removed by the author.
ReplyDeleteGood Explanation..Thanks
ReplyDelete