Inheritance is Fun

Inheritance is one of the most important aspect of the Object Oriented Programing. When I conduct interview, I have observed that Just one right example of Inheritance is enough to confuse the developers – even experienced ones.

Inheritance is like brain exercise. I always enjoy working on complex examples of inheritance. In today’s blog post, we will see such examples of inheritance.


Example 1

class A
{
    public void Show() => Console.WriteLine("A.Show");
}

class B : A
{
}

new B().Show();

Output

A.Show

Simple right ? Lets make the examples complex gradually.


Example 2

class A
{
public virtual void Print() => Console.WriteLine("A.Print");
}

class B : A
{
public override void Print() => Console.WriteLine("B.Print");
}

A obj = new B();
obj.Print();

Output:

B.Print

Example 3:

class A
{
public virtual void Show() => Console.WriteLine("A.Show");
}

class B : A
{
public new void Show() => Console.WriteLine("B.Show");
}

A obj = new B();
obj.Show();

Output:

A.Show

Example 4:

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

A obj = new B();
Console.WriteLine(obj.x);

Output:

1

Example 5

class A
{
  public A() => Console.WriteLine("A()");
}

class B : A
{
  public B() => Console.WriteLine("B()");
}

var obj = new B();

Output:

A()
B()

Example 6

class A
{
  public A() => Show();
  public virtual void Show() => Console.WriteLine("A.Show");
}

class B : A
{
  public override void Show() => Console.WriteLine("B.Show");
}

new B();

Output:

B.Show

Example 7

class A
{
  public class Nested
  {
    public virtual void Print() => Console.WriteLine("A.Nested");
  }
}

class B : A
{
  public class NestedB : A.Nested
  {
    public override void Print() => Console.WriteLine("B.Nested");
  }
}

A.Nested obj = new B.NestedB();
obj.Print();

Output:

B.Nested

Example 8

class A
{
  public virtual void Run() => Console.WriteLine("A.Run");
}

class B : A
{
  public new void Run() => Console.WriteLine("B.Run");
}

A obj = new B();
obj.Run();

Output:

A.Run

Example 9

class A
{
  public virtual void Print() => Console.WriteLine("A");
}

class B : A
{
  public new virtual void Print() => Console.WriteLine("B");
}

class C : B
{
  public override void Print() => Console.WriteLine("C");
}

A obj = new C();
obj.Print();

Output:

A

Why?
Because C overrides B.Print, but A never sees B’s version since B hid it (using new).
So polymorphism goes all the way back to A.


Example 10

class A
{
  public virtual string Name => "A";
}

class B : A
{
  public new string Name => "B";
}

A obj = new B();
Console.WriteLine(obj.Name);

Output:

A

Example 11

class A
{
  public void Execute() => Run();
  public virtual void Run() => Console.WriteLine("A.Run");
}

class B : A
{
  public override void Run() => Console.WriteLine("B.Run");
}

new B().Execute();

Output:

B.Run

Example 12: Now it gets… truly fun.

class A
{
  public class Inner
  {
    public virtual void Show() => Console.WriteLine("A.Inner");
  }
}

class B : A
{
  public class InnerB : Inner
  {
    public override void Show() => Console.WriteLine("B.Inner");
  }
}

class C : B
{
  public new class Inner : InnerB
  {
    public override void Show() => Console.WriteLine("C.Inner");
  }
}

A.Inner obj = new C.Inner();
obj.Show();

Output:

C.Inner

Example 13

class A
{
  public virtual void Show() => Console.WriteLine("A");
}

class B : A
{
  public new void Show() => Console.WriteLine("B");
}

class C : B
{
  public new void Show() => Console.WriteLine("C");
}

A obj = new C();
obj.Show();

Output:

A


Example 14:

class A
{
  public class X
  {
    public virtual void Display() => Console.WriteLine("A.X");
  }
}

class B : A
{
  public new class X : A.X
  {
    public override void Display() => Console.WriteLine("B.X");
  }
}

A.X obj = new B.X();
obj.Display();

Output:

B.X

Example 15:

class A
{
  public class Level
  {
    public virtual void DoSomething() => Console.WriteLine("A.Level");
  }
}

class B : A
{
  public class LevelB : Level
  {
    public override void DoSomething() => Console.WriteLine("B.Level");
  }
}

class C : B
{
  public new class Level : LevelB
  {
    public override void DoSomething() => Console.WriteLine("C.Level");
  }
}

A.Level obj = new C.Level();
obj.DoSomething();

Output:

C.Level

I think this is enough to ruin your day 😜. Have a good day.

Home » Inheritance is Fun