What is Inheritance in C#

No.of Views1288
Bookmarked0 times
Downloads 
Votes0
By  gurumatrix2004   On  16 Feb 2010 03:02:51
Tag : CSharp , Miscellaneous
What is Inheritance in C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In 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 Inheritance

Image Loading

Types of Inheritance in C#

  • Implementation Inheritance
  • Multiple Inheritances (Interface Inheritance) 

Implementation Inheritance

One base class (super class) and one derived class (sub class). 

Example:

Image Loading

Interface Inheritance

An 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:

Image Loading

Implementation inheritance code example

using 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 example

using 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");
}

}
}
 

 

Conclusion

In this article i have given the detail about inheritance in .NET. and there was two type of implementaion.

 
Sign Up to vote for this article
 
About Author
 
gurumatrix2004
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-11 Aug 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top