Inheritance is the process through which a Class derives properties from another Class. A Class that inherits the Properties of another Class is called a Child Class or Derived Class, Whereas , the Class from which the Child Class is derived is known as a Parent Class or Base Class.
A Parent Class is at higher level in the Class hierarchy. A Class hierarchy defines logical structuring of the Classes such that a general Class is on the top of the Class hierarchy and more Specialized Classes over at lower level.
Inheritance is one of the Primary pillar of Object Oriented Programming .
Following are some of the useful points regarding to Inheritance.
Single Inheritance :- Single Inheritance means a derived class can have only one Base Class.
Multilevel Inheritance :- Multilevel Inheritance means a child class is derived from a class , which in turn is derived
from another class.
public class RoadTransport : Transport
{
public string VechiecleNumber;
public string NameOfDriver;
public string DriverContactNumber;
public int CapacityOfVechiecle;
}
public class Transport
{
public string DepartmentId;
public string Head;
public string Location;
public string TypesOfTransport;
public int NumberOfVechiecles;
public int ReturnVechiecleCounts(string TypesOfTransport)
{
int availableVechiecleCounts=0;
if(TypesOfTransport=="Road")
{
availableVechiecleCounts = 15;
}
if (TypesOfTransport == "Air")
{
availableVechiecleCounts = 2;
}
if (TypesOfTransport == "Train")
{
availableVechiecleCounts = 6;
}
return availableVechiecleCounts;
}
}
internal class Program
{
static void Main(string[] args)
{
RoadTransport roadTransport = new RoadTransport();
int availableVechiecleCounts = roadTransport.ReturnVechiecleCounts("Road");
Console.WriteLine("Number of Vehicles available for Road :-" +
availableVechiecleCounts);
Console.WriteLine("Number of Vehicles available for Air :-" +
roadTransport.ReturnVechiecleCounts("Air"));
Console.ReadKey();
}
}
public class RoadTransport : Transport
{
public string VechiecleNumber;
public string NameOfDriver;
public string DriverContactNumber;
public int CapacityOfVechiecle;
public int ReturnVechicleCapacity(string VechiecleNumber)
{
int capacity = 0;
if(VechiecleNumber=="MP09 UT23**")
{
capacity = 45;
}
if (VechiecleNumber == "MP09 ZM12**")
{
capacity = 55;
}
if (VechiecleNumber == "MP09 PL3**8")
{
capacity = 30;
}
return capacity;
}
}
public class Driver : RoadTransport
{
public int SupervisorId;
public string SupervisorName;
public int RouteNumber;
public string timings;
public string LicenseNumber;
}
public class Transport
{
public string DepartmentId;
public string Head;
public string Location;
public string TypesOfTransport;
public int NumberOfVechiecles;
public int ReturnVechiecleCounts(string TypesOfTransport)
{
int availableVechiecleCounts=0;
if(TypesOfTransport=="Road")
{
availableVechiecleCounts = 15;
}
if (TypesOfTransport == "Air")
{
availableVechiecleCounts = 2;
}
if (TypesOfTransport == "Train")
{
availableVechiecleCounts = 6;
}
return availableVechiecleCounts;
}
}
internal class Program
{
static void Main(string[] args)
{
Driver objDriver = new Driver();
int availableVechiecleCounts = objDriver.ReturnVechiecleCounts("Road");
Console.WriteLine("Capacity of MP09 UT23** is :-" +
objDriver.ReturnVechicleCapacity("MP ** UT23**"));
Console.WriteLine("Capacity of MP09 PL3**8 is :-" +
objDriver.ReturnVechicleCapacity("MP09 PL3**8"));
Console.WriteLine("Number of Vechiecles available for Road :-" +
availableVechiecleCounts);
Console.WriteLine("Number of Vechiecles available for Air :-" +
objDriver.ReturnVechiecleCounts("Air"));
Console.ReadKey();
}
}
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.