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.ShowSimple 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.PrintExample 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.ShowExample 4:
class A { public int x = 1; }
class B : A { public new int x = 2; }
A obj = new B();
Console.WriteLine(obj.x);Output:
1Example 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.ShowExample 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.NestedExample 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.RunExample 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:
AWhy?
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:
AExample 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.RunExample 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.InnerExample 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.XExample 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.LevelI think this is enough to ruin your day 😜. Have a good day.