Simple inheritance in C#

In the following code we present a simplest example of inheritance in C#.

The class BaseClass is the parent class from which a child class ChildClass is derived

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace ConsoleApplication6
{
    class BaseClass
    {
        public void MyMethod()
        {
            Console.WriteLine("This is base class method");
        }

    }

    class ChildClass : BaseClass
    {
        public void MyChildMethod()
        {            
            Console.WriteLine("This is child class method");
        }

        public void MyChildMethod2()
        {
            base.MyMethod();
            Console.WriteLine("This is child class method");
        }
    }

    
    class Program
    {
        static void Main(string[] args)
        {
          //  BaseClass b = new BaseClass();
          //  b.MyMethod();

            ChildClass c = new ChildClass();
          //  c.MyChildMethod();
            c.MyChildMethod2();
        }        
        
        
    }
}

 

Add a Comment

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