Inheritance is an important pillar of oops. By using this property in C# we can use all features from the base class in any other classes. We can reuse the code by using inheritance.
Some important things about inheritance: -
When we have to do inherit any class then we use " : " (colon)
Syntax: - class <derived class name> : <base class name>
We cannot inherit the private members, function, methods, etc.
The class whose features are inherited is known as a super class (or a base class or a parent class)
The class that inherits the other class is known as sub class (or a derived class, extended class, or child class)
Code: -
using System;
public class Employee
{
public float salary = 40000;
}
public class Programmer: Employee
{
public float bonus = 10000;
}
class TestInheritance{
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
float Total_Salary= p1.salary+p1.bonus;
public class Employee
{
public float salary = 40000;
}
public class bonusEmployee: Employee
{
public float bonus = 10000;
public void Get_total()
{
float Total= base.salary+bonus;
Console.WriteLine("Toatal salary {0}", Total);
}
}
class TestInheritance{
public static void Main(string[] args)
{
bonusEmployee p1 = new bonusEmployee();
p1.Get_total();
}
}
}
Liked By
Write Answer
What is inheritance?
Join MindStick Community
You have need login or register for voting of answers or question.
Om ji mishra
14-Oct-2019Inheritance is an important pillar of oops. By using this property in C# we can use all features from the base class in any other classes. We can reuse the code by using inheritance.
Some important things about inheritance: -