C# Inheritance Introduction


This entry is part 1 of 10 in the series C# Inheritance

The post C# Introduction lists all of the C# series of posts we have at this site.

Inheritance is the second pillar of OOP. Inheritance is a kind of relationship between two classes that allows one class to inherit code from the other class. The “child” class now has all of the functionality of the “parent” class. The child imports functionality from the parent. The child class will also have its own functionality. It is sometimes referred to as is a behaviour, but this expression is only a guideline that does not always apply.

If you are new to inheritance, before digging deep into inheritance, I recommend first having a look at our Class Association posts.

The parent class is sometimes called the base class. The child class, which inherits from the parent is sometimes called to derived class. Therefore, the members of aa derived class consist of the members of its own declaration and the members of the base class. The derived class is said to extend the base class because it includes the members of the base class plus any functionality provided by its own declaration.

This code example is based on a course at Udemy.com by Mosh Hamedani. When designing inheritance it is very important to separate the shared stuff from the specific stuff. What are the items that all presentation objects have? They might all have, Cut, Copy, Bring to Front and Send To Back.

        public class PresentationObject
        {
             // common shared stuff
        }

        public class TextObject : PresentationObject
        {
            // stuff specific to text
        }

Let’s add code to PresentationOnbect.

    class PresentationObject
    {
        public int Width { get; set; }
        public int Height { get; set; }

        public void Copy()
        {
            Console.WriteLine("presentation object copied");
        }

    }

Let’s add code to Text and have Text inherit from PresentationObject.

    class Text : PresentationObject
    {
        public int FontSize { get; set; }
        public int FontName { get; set; }

        public void AddHyperlink(string url)
        {
            Console.WriteLine("Added hyperlink ");
        }
    }

In our main program all we are going to do is create our Text objeect.

    class Program
    {
        static void Main(string[] args)
        {
            var text = new Text();
            text.

            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }

Here is the top of the list. This Visual Studio feature of Intellisense is called List Members.

Here is the bottom of the list.

We can see all of the available members in the IDE when we type text dot. We can see members of the class Text inherited from (parent). We see the members of Text itself, which are FontSize, FontName and AddHyperlink. So we have 3 sets of members. All objects automatically inherit from the Object base class, even if you don’t specify that inheritance.

Series NavigationC# Composition >>