C# Inheritance Tutorial with Programming Example

inheritance-1
In this chapter you will learn

  • Basic Understanding of Inheritance
  • Base and Derived Class
  • Programming Example

Basic Understanding of Inheritance

Inheritance is the one of the important pillar of Object Oriented Programming (OOP). In this chapter you will understand Inheritance in simple and easy language with real world example.

What is Inheritance?

Inheritance allows you to access members of base class in child class. It enables you to create a child class that can access and use all the functionality of its base class. This way you can keep common variable and functions in a base class and use them as many times as you want in child class. Code reusability makes your program simpler and efficient.

Didn’t understand? Don’t worry; take a look of this real world example.

    1. Your son/daughter looks like you and sometimes behaves like you but they have also own properties and behavior. It means your son/daughter has inherited your quality and mixed them with their own quality.
    2. Consider an example of Vehicle. All the vehicles need Tyres in their wheels. So, the manufacturer designs their vehicle as they want and import Tyre from its base class and fit into Wheels. They don’t need to design Tyre all the time. Once they designed the model and use it various times.

    With the above two examples I just want to show you the concept of Inheritance.

    How Inheritance works in Programming?

    Once again take the example of Vehicle but in a programming manner. This program shows how inheritance works in programming.

    This program has a Tyre class which is base class and has a member method TyreType() which shows message that Tyre is Tubeless. There is also two class created Scooter and Car which inherits the functionality of Tyre class.

    Output

    Scooter Color is Red
    This is Tubeless Tyre
    Car Type : Ferrari
    This is Tubeless Tyre
    _

    Initialization of Base class and Derived Class

    Any class can be a base class. There is no dedicated rule for creating base class. When creating child class you can simple inherit base class followed by colon ( : ) symbol.

    Inheritance

    1. Inheritance
    2. Member Access
    3. Abstract and Virtual Methods
    4. constructors and Inheritance
    5. Multiple Inheritance
    6. Sealed Inheritance
    7. Example
    8. Exercises

    Summary

    In this chapter you understood Inheritance with a suitable programming example and real world scenario. In the next chapter you will learn Member Access in Inheritance.


    Leave a Reply

    Your email address will not be published. Required fields are marked *