Inheritance Interview Questions

Why multiple inheritance is not supported in C#?

To avoid ambiguity and Diamond problem, Consider a scenario where A, B, C and D are four classes. If B and C class is derived from A class, now if class D is derived from Class B and Class C using Multiple inheritance, now if class D wants to call common method which is implemented in Class B and Class C. so, there will be ambiguity to call the method of Class B or C class.

Also, multiple inheritances do complicate the design which can cause to other problems. So, to reduce the complexity and simplify of the language C# does not support multiple inheritance.


What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassA();
        ClassB classB = new ClassB();
        ClassC classC = new ClassC();
        ClassD classD = new ClassD();

        classA.Print();
        classB.Print();
        classC.Print();
        classD.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public virtual void Print()
    {
        Console.WriteLine("ClassC");
    }
}
public class ClassD : ClassC
{
    public override void Print()
    {
        Console.WriteLine("ClassD");
    }
}

Output of the above program is

ClassA
ClassB
ClassC
ClassD


What is output of above program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassB();
        ClassB classB = new ClassC();
        ClassC classC = new ClassC();

        classA.Print();
        classB.Print();
        classC.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public void Print()
    {
        Console.WriteLine("ClassC");
    }
}

Output of the above program is

ClassA
ClassB
ClassC

What is the he output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassB();
        ClassB classB = new ClassA();
        ClassC classC = new ClassB();

        classA.Print();
        classB.Print();
        classC.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public void Print()
    {
        Console.WriteLine("ClassC");
    }
}

It will give compile time error as we are trying to assign the parent to child

ClassB classB = new ClassA();
ClassC classC = new ClassB();

“ClassB” is derived from “ClassA” so here we are trying parent class to access the child class which is not possible


What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassB();
        ClassB classB = new ClassC();
        ClassC classC = new ClassC();

        classA.Print();
        classB.Print();
        classC.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public override  void Print()
    {
        Console.WriteLine("ClassC");
    }
}

Output of the above program is

ClassA
ClassC
ClassC

What is output of the below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassA();
        classA.Print();
        classA = new ClassB();
        ClassB classB = new ClassC();
        ClassC classC = new ClassC();

        classA.Print();
        classB.Print();
        classC.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public override  void Print()
    {
        Console.WriteLine("ClassC");
    }
}

Output of the above program is

ClassA
ClassB
ClassC
ClassC

What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassB();
        ClassB classB = new ClassC();
        ClassC classC = new ClassD();
        ClassD classD = new ClassD();
        classA.Print();
        classB.Print();
        classC.Print();
        classD.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public new void Print()
    {
        Console.WriteLine("ClassC");
    }
}

public class ClassD : ClassC
{
    public new void Print()
    {
        Console.WriteLine("ClassD");
    }
}

Output of below program is

ClassB
ClassB
ClassC
ClassD

What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassD();
        ClassB classB = new ClassC();
        ClassC classC = new ClassD();
        ClassD classD = new ClassD();
        classA.Print();
        classB.Print();
        classC.Print();
        classD.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public virtual void Print()
    {
        Console.WriteLine("ClassC");
    }
}

public class ClassD : ClassC
{
    public override void Print()
    {
        Console.WriteLine("ClassD");
    }
}

Output of the above program is

ClassB
ClassB
ClassD
ClassD

What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassD();
        ClassB classB = new ClassC();
        ClassC classC = new ClassD();
        ClassD classD = new ClassD();
        classA.Print();
        classB.Print();
        classC.Print();
        classD.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public override void Print()
    {
        Console.WriteLine("ClassC");
    }
}

public class ClassD : ClassC
{
    public override void Print()
    {
        Console.WriteLine("ClassD");
    }
}

Output of the above program is

ClassD
ClassC
ClassD
ClassD

What is the output of below program?

using System;

class Program
{

    static void Main()
    {
        ClassA classA = new ClassD();
        ClassB classB = new ClassC();
        ClassC classC = new ClassD();
        ClassD classD = new ClassD();
        classA.Print();
        classB.Print();
        classC.Print();
        classD.Print();
        Console.ReadLine();
    }
}

public class ClassA
{
    public virtual void Print()
    {
        Console.WriteLine("ClassA");
    }
}
public class ClassB : ClassA
{
    public override void Print()
    {
        Console.WriteLine("ClassB");
    }
}
public class ClassC : ClassB
{
    public new void Print()
    {
        Console.WriteLine("ClassC");
    }
}

public class ClassD : ClassC
{
    public virtual void Print()
    {
        Console.WriteLine("ClassD");
    }
}

Output of the above program is

ClassB
ClassB
ClassC
ClassD

Helpful Resource

Leave a Reply

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