IntroductionIn object-oriented programming (OOP), inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. Inheritance is employed to help reuse existing code with little or no modification. The new classes, known as Sub-classes (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as Super-classes (or ancestor classes). The inheritance relationship of sub- and superclasses gives rise to a hierarchy. What is Inheritance?- Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (concept) of object-oriented programming
- Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes
- The Class whose methods and variables are defined is called super class or base class
- The Class that inherits methods and variables are defined is called sub class or derived class
- Sometimes base class known as generalized class and derived class known as specialized class
- Keyword to declare inheritance is “:” (colon) in visual c#
Benefits of using Inheritance- Once a behavior (method) or property is defined in a super class(base class),that behavior or property is automatically inherited by all subclasses (derived class).
- Code reusability increased through inheritance
- Inheritance provide a clear model structure which is easy to understand without much complexity
- Using inheritance, classes become grouped together in a hierarchical tree structure
- Code are easy to manage and divided into parent and child classes
Example of InheritanceTypes of Inheritance in C#- Implementation Inheritance
- Multiple Inheritances (Interface Inheritance)
Implementation InheritanceOne base class (super class) and one derived class (sub class). Example: Interface InheritanceAn interface looks like a class, but has no implementation. It contains definitions of events, indexers, methods and properties. An interface inherited by classes An interface inheritance defined with keyword “interface”. In C# Interface Inheritance also known as multiple inheritances. Inheritance Tree Structure:Implementation inheritance code exampleusing System;
using System.Web;
public class Person
{
string _name;
string _address;
string _dob;
string _cast;
public string Name
{
set { _name = value; }
get { return _name; }
}
public string Address
{
set { _address = value; }
get { return _address; }
}
public string DOB
{
set { _dob = value; }
get { return _dob; }
}
public string CAST
{
set { _cast = value; }
get { return _cast; }
}
public virtual void Profession()
{
Console.WriteLine("Nothing");
}
}
public class Student : Person
{
string _institutename;
string _no;
public string NameofInstitute
{
set { _institutename = value; }
get { return _institutename; }
}
public virtual void Class()
{
Console.Write("Nothing to display");
}
public string StudentNumber
{
set { _no = value; }
get { return _no; }
}
public override void Profession()
{
Console.WriteLine("Im Student");
}
}
class SchoolStudent:Student
{
public override void Class()
{
Console.Write("Class 7th Standard");
}
}
class CollegeStudent : Student
{
public override void Class()
{
Console.Write("B.SC Physics");
}
}
class Employee:Person
{
string _companyname;
int _employeeid;
double _salaryamount;
public string Companyame
{
set { _companyname = value; }
get { return _companyname; }
}
public int EmployeeId
{
set { _employeeid = value; }
get { return _employeeid; }
}
public double SalaryAmount
{
set { _salaryamount = value; }
get { return _salaryamount; }
}
public override void Profession()
{
Console.WriteLine("Im Employee");
}
public virtual void Designation()
{
Console.Write("No Specified");
}
public virtual void Qualification()
{
Console.Write("No Specified");
}
}
class Worker:Employee
{
public override void Qualification()
{
Console.Write("12th pass");
}
public override void Designation()
{
Console.Write("Worker");
}
}
class SoftwareDeveloper : Employee
{
public override void Qualification()
{
Console.Write("MCA in Computers");
}
public override void Designation()
{
Console.Write("SoftwareDeveloper");
}
} Interface inheritance code exampleusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
interface IPerson
{
string Name
{
set;
get;
}
string Address { set; get; }
string DOB { set; get; }
string CAST { set; get; }
void Profession();
}
public abstract class AbPerson
{
string Abstract_Name
{
set;
get;
}
public abstract void Abstract_Profession();
}
public class Person:AbPerson,IPerson
{
string _name;
string _address;
string _dob;
string _cast;
public string Name
{
set { _name = value; }
get { return _name; }
}
public string Address
{
set { _address = value; }
get { return _address; }
}
public string DOB
{
set { _dob = value; }
get { return _dob; }
}
public string CAST
{
set { _cast = value; }
get { return _cast; }
}
public virtual void Profession()
{
Console.WriteLine("Nothing");
}
public override void Abstract_Profession()
{
Console.WriteLine("Nothing");
}
}
}
ConclusionIn this article i have given the detail about inheritance in .NET. and there was two type of implementaion. |