Abstract Class Interview Questions

Below are the Abstract Class Interview Questions for beginners and experienced developers for Interview Preparation.

What is the need for abstract classes?

Abstract classes allow partial implementations of a methods in a class Which is mostly used for an extension the sub class. Where parent class has default implementation and class specific implementation can be done in the child class. Abstract class promotes code reusability and loose coupled code.


Why abstract class cannot be instantiated?

Abstract class can contain abstract method as well as non-abstract method. So, we can say that abstract class is always partially implemented. Instantiating the partially implemented method can cause other failures for example, if CLR allow us to create an instance of abstract class then created object can call non-abstract method as well as abstract method. Calling abstract method which is unable to execute at runtime will create failure in CLR hence abstract class cannot be instantiated.


What is the issue below code snippet?

public abstract class SuperClass
{
    public abstract void Print()
    {
        Console.WriteLine("SuperClass Print ");
    }
}

public class ChildClass : SuperClass
{
    public override void Print()
    {
        Console.WriteLine("ChildClass Print ");
    }
}

In above code snippet the Print() method is declared as abstract and also has the body, hence compiler will generate compile time error as abstract method should not have body.


What are advantages abstract class has over the interface?

  • Abstract class are used for providing common functionality where as interface are used to defining contract between the class.
  • Abstract class can contain abstract as well as not abstract methods, interface can only contain the definition like abstract method
  • Abstract class member can specify access modifier to a method, interface cannot use access specifiers
  • Abstract class can contain constructor where as interface cannot contain constructor.

How to force child classes to provide method implementations for virtual methods?

Abstract classes can be used to force child classes to provide new method implementations for virtual methods. For example, let’s say we have below existing SuperClass with virtual print method.

public class SuperClass
{
    protected virtual void Print()
    {
       Console.WriteLine("SuperClass Print ");
    }
}

Now if we want to enforce this method implementation in child class without changing the existing implementation then can create a abstract class which inherits from above class refer code snippet below.

public abstract class AbstractClass : SuperClass
{
    public abstract void Print();
}

Now we will inherit all child class with the above abstract class

public class ChildClass : AbstractClass
{
    public override void Print()
    {
        Console.WriteLine("ChildClass Print ");
    }
}

This implementation will enforce the base class virtual methods to be implemented in child class.


When to use abstract class?


In development where there is need to create multiple versions of the component and when a sub class need to fulfil similar behaviour with different functionality then we should use abstract class. As versioning is not a problem with abstract class. We can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.


What is the issue below code snippet?

public abstract class SuperClass
{
    static abstract void Print();
}

In the above code snippet abstract method Print() marked as static, abstract method can not be marked as static cause it does not have definition. Moreover, the static method cannot be overridden therefor compile will generate compile-time error.


why static methods cannot be overridden?


Static methods belong to a particular class and not to its instance. So, If we try to override a static method, we will not get any compilation or runtime error, but compiler would just hide the static method of base class.


Abstract class vs Interface?

abstract class vs interface c#,  when to use abstract class vs interface c#, which is faster abstract class or interface in c# , when to use abstract class in c#

What is the output of below code snippet?

class Program
{
    static void Main()
    {
        SuperClass childClass = new ChildClass();
        childClass.Print();
        Console.ReadLine();
    }
}


public abstract class SuperClass
{
    public abstract void Print();
}

public class ChildClass : SuperClass
{
    public void Print()
    {
        Console.WriteLine("ChildClass Print ");
    }
}

The above code will give us compile-time error, as print method does not have overridden keyword hence child class does not have definition for print abstract method.
‘ChildClass’ does not implement inherited abstract member ‘SuperClass.Print()’

To fix the error we have change implementation of the above code as per below code snippet.

public class ChildClass : SuperClass
{
    public override void Print()
    {
        Console.WriteLine("ChildClass Print ");
    }
}


If you have any Abstract Class Interview Questions please share in comments also you can read more about on below topics:

Leave a Reply

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