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();
}
}
}
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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: -