Inheritance

It can inherit from another class to extend or customize the original class. It allows you to reuse the functionality from the base class. A class can inherit from single class but it can be inherited by many classes.

Inheritance not only promotes code reuse but also helps to organize code into hierarchical structures.

public class Contact
{ ... }
public class BusinessContact : Contact
{ ... }
public class ProfessionalContact : Contact
{ ... }
public class PersonalContact : Contact
{ ... }
//base class
public class Asset
{
	public string Name;
}
//inherits from base class
public class Stock : Asset
{
	public long SharesOwned;
}

Derived classes (subclass) are classes which inherits the base class.

Base class is also called super class or the class that gets inherit from.

It is possible to cast operators to test or convert a base type to derived type.

Note

Sometimes it is better to introduce new virtual methods on the base class and implement it in the derived type.

It is important not to overuse inheritance and where possible use abstract class.

Hiding Inherited Members

Sometimes you want to hide the member from base class and redefine the same member in subclass. The new modifier allows you to obscure the base class member and use the subclass. You will need to add the keyword new to redefine the member from the base class into subclass.

using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
           
           B b = new B();
            b.Counter.Dump();
        }       

    }
    
        public class A 
        {
            public int Counter = 1;
        }
    
        public class B : A 
        {
            public new int Counter = 2;
        }
    
}

Constructors and Inheritance

A subclass must declare its own constructor. The base class constructor are accessible to the derived class but never automatically inherited. A subclass needs to define its own constructor thus it can call the base class constructor with the base keyword. Base class constructors always execute first to ensure that base initialization occurs before specialized initialization.

static void Main()
{
	new Subclass (123);
}

public class Baseclass
{
	public int X;
	public Baseclass () { }
	public Baseclass (int x) { this.X = x; }
}

public class Subclass : Baseclass
{
	public Subclass (int x) : base (x) { }
}


Implicit calling of the parameterless base-class constructor

If the subclass constructor misses the base keyword then the base class parameterless constructor is applied. If there is no parameterless constructor than subclasses base keyword is important.

static void Main()
{
	new Subclass();
}

public class BaseClass
{
	public int X;
	public BaseClass() { X = 1; }
}

public class Subclass : BaseClass
{
	public Subclass() { Console.WriteLine (X); }  // 1
}

Constructor and field initialization order
Once the object is instantiated, the following order of initialization takes place:
1. From subclass to base class: fields initialization and argument base-class constructors evaluation.
2. From base class to subclass: constructor bodies execute.

 public class A 
        {
            //Executes 3rd
            int x = 1;
            public A (int x)
            {
                //Executes 4th
                ....
            }
        }
    
        public class B : A 
        {
            //Executes 1st
            int y = 1;
            //Executes 2nd
            public B (int x): base (x + 1)
            {
                //Executes 5th
                .....
            }
        }

Overloading and Resolution

When overloading is called the most specific type has precedence. The overload is determined during compile time rather than at run time.

// When calling an overload method, the method with the most specific 
// parameter type match has precedence, based on the *compile-time* variable type:

static void Main()
{
	Foo (new House());		// Calls Foo (House)
	
	Asset a = new House();
	Foo (a);				// Calls Foo (Asset)
}

static void Foo (Asset a) { "Foo Asset".Dump(); }
static void Foo (House h) { "Foo House".Dump(); }

public class Asset
{
	public string Name;
}

public class Stock : Asset   // inherits from Asset
{
	public long SharesOwned;
}

public class House : Asset   // inherits from Asset
{
	public decimal Mortgage;
}

Related Article

Polymorphism
Casting and Reference Conversions
Virtual
Abstract
Virtual vs Override
New vs Override
Sealed
Base Keyword

Leave a Reply

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