DEV Community

Kiron Roy
Kiron Roy

Posted on • Updated on

C# Inheritance Basics

  • Inheritance should be used sparingly
  • The is-a relationship is a good indicator if one class can inherit from another
  • However it is not the whole story. A boat truck has similar properties as a car (doors, start engine, stop engine) but it is not a car.

Below is some sample code. Note that all the classes are in one file for simplicity. Classes should be moved to separate files.

using System;

namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            // Boat is a stand alone class does not inherit from Vehicle 
            Bertram bertram = new Bertram();
            bertram.BuoyancyFactor();

            // Honda motorcyle IS-A motorcycle
            HondaMotorcycle honda = new HondaMotorcycle();
            honda.Logo = "Honda";

            // Tesla car IS-A car
            // A car IS-A vehicle
            Tesla tesla = new Tesla();
            //tesla. access properties from Vehicle AND Car class
            tesla.isElectricPowered = true;


            Console.ReadLine();
        }
    }

    public class Bertram : Boat
    {

    }

    public class Boat 
    {
        // stand alone class, NO "IS-A" relationship to vehicle  
        // helper function to create a start and stop engine which is found in Vehicle class

        public void BuoyancyFactor()
        {
            Console.WriteLine("Very buoyant!");
        }
    }

    public class Tesla : Car
    {
        public bool isElectricPowered { get; set; }
    }

    public class Car : Vechile
    {
        public int doors { get; set; }
    }


    public class HondaMotorcycle : Motorcycle
    {
        public string Logo { get; set; }
    }

    public class Motorcycle : Vechile
    {
        public void FrontWheel()
        {

        }

        public void BackWheel()
        {

        }
    }

    public class Vehicle
    {
        public void StartEngine()
        {

        }

        public void StopEngine()
        {

        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Discussion (6)

Collapse
luturol profile image
Rafael Ahrons

Besides inheriting from another class you can have it implement multiple interfaces!!

Keep going. Inheritance is magical in C#

Collapse
kironroy profile image
Kiron Roy Author

Thanks. I am writing an article about inheritance and interfaces working together.

Kind Regards,
Kiron Roy

Collapse
wrldwzrd89 profile image
Eric Ahnell

I've heard a lot of advice leaning towards "has-a" relationships over "is-a" class inheritance. This is useful for explaining the fundamentals. Thanks for posting!

Collapse
kironroy profile image
Kiron Roy Author • Edited on

Thank you for enjoying the post. I am still learning my self.

I heard of "has-a" "is-a" when it comes to Ruby, but didn't hear about it in C#. Also there is no multiple-inheritance in C#, which is a relief. Moreover, the is-a relationship is not the entire story when it comes to inheritance. If there is noting to inherit, one simply doesn't. Just create another class.

Kind Regards,
Kiron Roy

Collapse
canman33 profile image
<coder2coder/>

There’s a pretty good book titled “The object-oriented thought process” by Matt Weisfeld that goes into depth about all this. It’s a pretty good reference I’m finding. Great article btw! Thank you!

Thread Thread
kironroy profile image
Kiron Roy Author

Thank you, I will check out the book