Inheritance in C#

In real world inheritance refers to the phenomena of adopting physical features, characteristics and wealth of parents. In C#, the concept is similar. A class can inherit properties of its parent class via inheritance. In C#, only single inheritance is possible which means that a class cannot inherit from multiple classes. However, multiple classes can inherit from one class. The class which is being inherited is called the parent or base class and the class which inherits the base class is called child or derived class.

Inheritance is handy where multiple classes have some as well as different characteristics. All the common characteristics are packaged into the parent class while the properties intrinsic to each individual class are encapsulated within that class. Consider example of a Car and Truck, both of them have some name, price and model. However truck can have a property called load capacity while car may have a property called seating capacity. In such cases the first three properties i.e name, price and model will be packaged into parent class Vehicle. The load capacity property will be encapsulated inside Truck class while seating capacity property will be enclosed inside a Car class. To inherit a class, the colon operator “:” is used followed by the name of the class to inherit. Have a look at the following example to understand the concept of inheritance in C#.

´╗┐using System;

namespace CSharpTutorials {
    public class Vehicle {

        public string name;
        public int price;
        public int model;

        public Vehicle(string vname, int vprice, int vmodel)  {
            name = vname;
            price = vprice;
            model = vmodel;
        }
    }

    public class Car : Vehicle {
        public int seating_capacity;

        // creating constructor and calling base class constructor as well
        public Car(string vname, int vprice, int vmodel, int capacity) : base(vname, vprice, vmodel) {
            seating_capacity = capacity;
        }

        public void DisplayInfo() {
            Console.WriteLine("\nThe name of the car is:" + name);
            Console.WriteLine("The price of the car is:" + price);
            Console.WriteLine("The model of the car is:" + model);
            Console.WriteLine("The seating capacity of car is:" + seating_capacity);
        }
    }

    public class Truck : Vehicle  {

        public int load_capacity;
        public Truck(string vname, int vprice, int vmodel, int load) : base(vname, vprice, vmodel) {
            this.load_capacity = load;
        }

        public void DisplayInfo() {
            Console.WriteLine("\nThe name of the Truck is:" + name);
            Console.WriteLine("The price of the Truck is:" + price);
            Console.WriteLine("The model of the Truck is:" + model);
            Console.WriteLine("The load capacity of Truck is:" + load_capacity +" tons.");
        }
    }

    class Tutorial {
        static void Main(string[] args) {
            // Creating objects of car and truck class
            Car car = new Car("Toyota", 750000, 2010, 5);
            Truck Truck = new Truck("HINO", 4200000, 2005, 300);

            car.DisplayInfo();
            Truck.DisplayInfo();
            Console.ReadKey();
        }
    }    
}

Download the code

<<< CSharp Class, ObjectInterfaces in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .