What is Inheritance?
Using the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.
The following are the types of inheritance in C#.
Types of Inheritance:
- Single Inheritance
- Hierarchical inheritance
- Multilevel inheritance
- Multiple inheritance(c# does not support it but we can do it by using interface)
The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.
Base class - is the class from which features are to be inherited into another class.
Derived class - it is the class in which the base class features are inherited.
Single Inheritance:
It is the type of inheritance in which there is one base class and one derived class. please see below example.Example:
//Single Inheritance
class Inheritance_Example
{
static void Main(string[] args)
{
Student s = new Student();
Console.WriteLine(s.StudentName ()+ " " + s.CourseName()); //we can call base class method from derived class.
Console.ReadLine();
}
}
public class College
{
public string CourseName()
{
return"C#";
}
}
public class Student : College
{
public string StudentName()
{
return "Roy";
}
}
Hierarchical inheritance
This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.Please see below example.we have inherit "College" class in "Student" and "Teacher" classes.
//Hierachical Inheritance
class Inheritance_Example
{
static void Main(string[] args)
{
Student s = new Student();
Console.WriteLine(s.StudentName ()+ " " + s.CourseName());
Teacher t = new Teacher();
Console.WriteLine(t.TeacherName() + " " + t.CourseName());
Console.ReadLine();
}
}
public class College
{
public string CourseName()
{
return"C#";
}
}
public class Student : College
{
public string StudentName()
{
return "Roy";
}
}
public class Teacher:College
{
public string TeacherName()
{
return "Dr Sara";
}
}
Multilevel inheritance
When one class is derived from another derived class then this type of inheritance is called multilevel inheritance. Please see below example.
Example
C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces.
Example
//Multivel Inheritance
class Inheritance_Example
{
static void Main(string[] args)
{
Student s = new Student();
Console.WriteLine(s.StudentName ()+ " " + s.CourseName());
Teacher t = new Teacher();
Console.WriteLine(t.TeacherName() +" - "+ t.StudentName()+ " - " + t.CourseName());
Console.ReadLine();
}
}
public class College
{
public string CourseName()
{
return"C#";
}
}
public class Student : College
{
public string StudentName()
{
return "Roy";
}
}
public class Teacher:Student
{
public string TeacherName()
{
return "Dr Sara";
}
}
Multiple inheritance using Interfaces
C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces.
//Multiple Inheritance
class Inheritance_Example
{
static void Main(string[] args)
{
College c = new College();
Console.WriteLine(c.TeacherName());
Console.WriteLine(c.StudentName());
Console.ReadLine();
}
}
public interface Student
{
string StudentName();
}
public interface Teacher
{
string TeacherName();
}
public class College:Student,Teacher
{
public string StudentName()
{
return "C#";
}
public string TeacherName()
{
return "Dr Sara";
}
}
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.