Books MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

C# Interview Questions on Inheritance



Inheritance - Video

Click here for video on Method Hiding
What are the 4 pillars of any object oriented programming language?
1.
Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism

Do structs support inheritance?
No, structs do not support inheritance, but they can implement interfaces.

What is the main advantage of using inheritance?
Code reuse

Is the following code legal?
class ChildClass : ParentClassA, ParentClassB
{
}

No, a child class can have only one base class. You cannot specify 2 base classes at the same time. C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.

What will be the output of the following code?
using System;
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("I am a base class");
}
}
public class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class");
}
static void Main()
{
ChildClass CC = new ChildClass();
}
}
Output:
I am a base class
I am a child class
This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructor executed before the ChildClass constructor.

Does C# support multiple class inheritance?
No, C# supports single class inheritance only. However classes can implement multiple interfaces at the same time.

10 comments:

  1. Why some argue that the use of inheritance should be minimized?This was one of the question asked to me on one of my interview?Can you please post this on your blog>

    ReplyDelete
    Replies
    1. Favor composition over inheritance. This is a design principle.

      Delete
  2. SIMPLE , IF U IMPLEMENT INTERFCAE THEN U HAVE TO IMPLEMENT ALL OF THEM ...THAT WHY IT SHOULD BE MINIMIZED.

    ReplyDelete
  3. Inheritance: Creating new classes from existing classes such that the new classes will acquire all the features of existing classes is called inheritane.
    example:class A
    {
    protected int a;
    protected int b;
    public void method1()
    {
    }
    }
    Note:here A is Base class or Super class, class A access only its members.
    class B:A
    {
    private int c;
    public void method2()
    {
    }
    }
    Note:B is a subclass and aceess the both class members.

    ReplyDelete
  4. class A are acceceble from class B class A is base class

    ReplyDelete
  5. I have 3 class with same method which are inherited for example
    Class A
    {
    Protected void getvalues()
    {
    Consol.WriteLine(“values of a”);
    }
    }
    Class B : A
    {
    Protected void getvalues()
    {
    Consol.WriteLine(“values of a”);
    }
    }
    Class C : B
    {
    Getvalues();
    Protected void getvalues()
    {
    Consol.WriteLine(“values of a”);
    }
    }

    can i call getvalues method of a class in c class

    ReplyDelete
    Replies
    1. Yes, of course. You can call the getvalues() method of class C as following :
      Class C:B
      {
      protected void Getvalues()
      {
      Console.WriteLine("Values of c.");
      }
      static void Main(string[] args)
      {
      C c = new C();
      c.Getvalues();
      }
      }
      if you want to invoke class B's getvalues() then Use
      static void Main(string[] args)
      {
      B b = new C();
      b.Getvalues();
      }
      but first make getvalues() of Class B as public.

      if you want to invoke class A's getvalues() then Use
      static void Main(string[] args)
      {
      A a = new C();
      a.Getvalues();
      }
      but first make getvalues() of Class A as public.

      Delete
  6. Can you please explain the outcome of his program? It was an interview question.
    class Hello
    {


    static void Main()
    {
    D d = new D();
    A a = new A();
    B b = new B();
    C c = new C();

    a.Amethod();
    b.Amethod();
    c.Cmethod();
    d.Cmethod();
    Console.ReadKey();
    }


    }

    public class A
    {
    public virtual void Amethod()
    {
    Console.WriteLine("A.F,");
    }
    }
    public class B : A
    {
    public override void Amethod()
    {
    Console.WriteLine("B.f,");
    }
    }
    public class C : B
    {
    public virtual void Cmethod()
    {
    Console.WriteLine("C.F,");
    }
    }
    public class D : C
    {
    public override void Cmethod()
    {
    Console.WriteLine("D.F,");
    }
    }
    }

    ReplyDelete
  7. I believe it will be following:
    A.F, B.f, B,f, D.F

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us