Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Inheritance in C#

C# Inheritance

In C#, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in other class.

In C#, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

Advantage of C# Inheritance is Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

Base and Derived Classes

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.

The syntax used in C# for creating derived classes is as follows −

 

 class  {
   ...
}

class  :  {
   ...
}

C# Single Level Inheritance

When one class inherits another class, it is known as single level inheritance. Let’s see the example of single level inheritance which inherits the fields from the base class.

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();  

        Console.WriteLine("Salary: " + p1.salary);  
        Console.WriteLine("Bonus: " + p1.bonus);  

    }  
}  

Output:

Salary: 40000
Bonus: 10000

In the above example, Employee is the base class and Programmer is the derived class.

C# Multi Level Inheritance

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C#. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Let’s see the example of multi level inheritance in C#.

 

using System;  
public class Animal  {  
    public void eat() { Console.WriteLine("Eating..."); }  
}  
public class Dog: Animal  {  
    public void bark() { Console.WriteLine("Barking..."); }  
}  
public class BabyDog : Dog  {  
    public void weep() { Console.WriteLine("Weeping..."); }  
}  
class TestInheritance2{  
    public static void Main(string[] args)  {  
        BabyDog d1 = new BabyDog();  
        d1.eat();  
        d1.bark();  
        d1.weep();  
    }  
} 

Output:

Eating...
Barking...
Weeping...

Try the following example in the editor below.

With reference to above example, create a derived class Rectangle inherited from class Shape, your task is to create a public function in it named GetArea which return the area of rectange.

Start solving Inheritance in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test