Inheritance in oops:
In this tutorial you will learn Inheritance,Purpose
of Inheritance and sample code. Getting the features of one class to
another class is known as inheritance.
The class which is giving features to
another class is known as base class or parent class.The class which getting
the features from another class is known as derived class or child class.
The actual mean of using inheritance in our application is ‘Code
reusability’ and code optimization.
Similarly in Object Oriented Programming,Inheritance is the process of inheriting properties of objects of one class
by objects of another class.
In case of inheritance if we create an object of derive class or child class we are able to call
our own method and variable and also we can call base class methods and variable,this is the cause we say that inheritance provide me code reusability.
For Example:
using System;
class Manager //Parent Class or
Base Class
{
public string
Mgr_name;
int Mgr_age;
string Mgr_address;
public string
departmant()
{
return "Assistant
Manager";
}
}
class Employee:Manager //Inheritance betweentwo classes
//Employee
is Derive class or child class
{
int Emp_age;
string Emp_name;
static void Main(string[]
args)
{
Employee emp = new Employee();
emp.Mgr_name = "ManagerName";
emp.departmant();
//we are able to call base class
variable and methods
emp.Emp_name = "EmpName";
emp.Emp_age = 25;
//we are able to call own(derive
class) variable and methods
}
}
Types of inheritance in oops
1) Single inheritance
2) Multilevel inheritance
3) Hierarchical inheritance
4) Multiple inheritance
5) Hybrid inheritance
Single inheritance:
In this inheritance, there is only one base class and one derived class.
Multilevel inheritance:
In this we have many base classes and many derived classes. The classes
which are acts as a base class as well as derived class known as intermediate
classes.
Hierarchical inheritance:
In this, there is no. of derived classes derived from one base class.
Multiple inheritance:
In this, one class derived from multiple base classes. Multiple
inheritance is not supported in c# in classes. We can achieve this by
interfaces.
Hybrid inheritance:
The combination of any two inheritances is known as hybrid inheritance.
Also Read:
Also Read:
best explanation
ReplyDeletePost a Comment