In this lesson, you will learn about one of the most important concepts in object-oriented programming, which is inheritance. You will learn how to create a based class and derive class, and how implement multi inheritance and sealed class in C#.

What is inheritance in C#?

Inheritance is an object-oriented programming concept that allows programmers to use members of a class from other class. It’s the mechanism of massing member from a parent to a child class, also known as base and derived class. Inheritance allows programmers to create reusable code functionalities, as well as   design software that can be scalable and maintained as requirements change over time. 

So, let’s imagine that you want to create classes to handle vehicles. A vehicle can be anything from a motorcycle to a car or bus. We all know that vehicles share too many specification and functionalities among them. So, how about having one class that can hold implements all these shared areas instead of repeating them in each different class? This is when inheritance gets involved. 

Inheritance diagram

inheritance diagram

Base and derived class

A base class is the class that act as a parent class of other classes. It’s a class that gives both its data and behavior to derived class, such as the vehicle class giving its members to the Car class. The base class doesn’t inherit from any other class. 

Syntax of creating base and derived classes

        public class Base_CLass
        {

        }


        class Derived_Class : Base_CLass
        {

        }

Example I of base and derived class in C#

    public class Vehicle
    {
        public Vehicle()
        { }
        protected string _type;
        protected string _engineType;
        protected double _mileage;
        protected void SetType(string type)
        {
            // such as car, bus, motorcycle.  
            _type = type;
        }
        protected void SetEngineType(string engineType)
        {
            // such as diesel, regular gas, unleaded gas 
            _engineType = engineType;
        }
        public void SetMileage(int mileage)
        {
            _mileage = mileage;
        }
    }

    public class Car : Vehicle
    {
        public double GetKilometrage()
        {
            return _mileage / 0.62137; // comes from the base class 
        }
    }

    public class Bus : Vehicle
    {
        private int capacity;
        public int Capacity
        {
            get
            {
                return capacity;
            }
            set
            {
                Capacity = value;
            }
        }

        public double GetMileage()
        {
            return _mileage; // comes from the base class 
        }

        public int GetCapacity()
        {
            return Capacity; // specific to a bus 
        }
    }

Now let’s create an instance of the class Car and use the derived method from class vehicle: 

 static void Main(string[] args)
        {
            Car myCar = new Car();
            myCar.SetMileage(25000);
            Console.WriteLine(myCar.GetKilometrage().ToString());
            Console.ReadKey();
        }

Output

40233.67

Example II of base and derived class in C#

  public class Employee
    {



        public Employee()
        { }

        public double salary;
        public double bonus;
        public static void Main(string[] args)
        {
            Architect architect = new Architect();
            architect.setSalary(10000);
            architect.setBonus(2000);
            Console.WriteLine("Salary: " + architect.salary);
            Console.WriteLine("Bonus: " + architect.bonus);
            Console.WriteLine("Total: " + architect.SetTotalSalary());
            Console.ReadKey();
        }
    }

    public class Architect : Employee
    {
        public void setSalary(double _salary)
        {
            salary = _salary;
        }
        public void setBonus(float _bonus)
        {
            bonus = _bonus;
        }
        public double SetTotalSalary()
        {
            return salary + bonus;
        }
    }

Output

Salary: 10000
Bonus: 2000
Total: 12000

Sealed Class

Sealed classes are non-inherited and used when a class needs to restrain inheritance from other classes. The sealed keywords in used when defining a seal class. 

Example of sealed class in C#

    sealed class SealedClass
    {
        // class body 
    }
Last modified: August 11, 2018

Comments

Write a Reply or Comment

Your email address will not be published.