C# Interview Questions on Abstract and Sealed Class Members


What is an abstract class?
An abstract class is an incomplete class and must be implemented in a derived class.

Can you create an instance of an abstract class?
No, abstract classes are incomplete and you cannot create an instance of an abstract class.

What is a sealed class?
A sealed class is a class that cannot be inherited from. This means, If you have a class called Customer that is marked as sealed. No other class can inherit from Customer class. For example, the below code generates a compile time error "MainClass cannot derive from sealed type Customer.
using System;
public sealed class Customer
{
}
public class MainClass : Customer
{
public static void Main()
{
}
}

What are abstract methods?
Abstract methods are methods that only the declaration of the method and no implementation.

Will the following code compile?
using System;
public abstract class Customer
{
public abstract void Test()
{
Console.WriteLine("I am customer");
}
}
public class MainClass
{
public static void Main()
{
}
}
No, abstract methods cannot have body. Hence, the above code will generate a compile time error stating "Customer.Test() cannot declare a body because it is marked abstract"

Is the following code legal?
using System;
public class Customer
{
public abstract void Test();
}
public class MainClass
{
public static void Main()
{
}
}

No, if a class has even a single abstract member, the class has to be marked abstract. Hence the above code will generate a compile time error stating "Customer.Test() is abstract but it is contained in nonabstract class Customer"

How can you force derived classes to provide new method implementations for virtual methods?
Abstract classes can be used to force derived classes to provide new method implementations for virtual methods. An example is shown below.
public class BaseClass
{
public virtual void Method()
{
// Original Implementation.
}
}

public abstract class AbstractClass : BaseClass
{
public abstract override void Method();
}

public class NonAbstractChildClass : AbstractClass
{
public override void Method()
{
// New implementation.
}
}

When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method. In the above example, Method() on class NonAbstractChildClass cannot call Method() on class BaseClass. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Can a sealed class be used as a base class?
No, sealed class cannot be used as a base class. A compile time error will be generated.

Will the following code compile?
public abstract sealed class Test
{
public virtual void Method()
{
}
}
No, a class cannot be marked as sealed and abstract at the same time. This is because by definition, a sealed class cannot be a base class and an abstract class can only be a base class.

8 comments:

  1. virtual methods has an implementation and provide the derived class with the option of overriding it.Abstract method does not provide an implementation and forces the derived class to override the method.pls explain in detail.

    ReplyDelete
  2. how can "an abstract class can only be a base class" since the code beneath shows very clear that "AbstractClass" is an abstract class DERIVED from "BaseClass"?

    public class BaseClass
    {
    public virtual void Method()
    {
    // Original Implementation.
    }
    }

    public abstract class AbstractClass : BaseClass
    {
    public abstract override void Method();
    }

    public class NonAbstractChildClass : AbstractClass
    {
    public override void Method()
    {
    // New implementation.
    }
    }

    ReplyDelete
  3. very good explanation of abstract method

    ReplyDelete
  4. very good to the peoples who s having a doubt in abstract class and methods..thanx a lot

    ReplyDelete
  5. nice examples to explain one of the most confusing Topic

    ReplyDelete
  6. Can you provide the real time example using Abstract class?

    ReplyDelete
  7. Hi Venkat,

    I have one Doubt.
    Abstract methods maybe or may not have body right. then why error will come for following

    Will the following code compile?
    using System;
    public abstract class Customer
    {
    public abstract void Test()
    {
    Console.WriteLine("I am customer");
    }
    }
    public class MainClass
    {
    public static void Main()
    {
    }
    }
    No, abstract methods cannot have body. Hence, the above code will generate a compile time error stating "Customer.Test() cannot declare a body because it is marked abstract"

    ReplyDelete
  8. How to access non-abstract method of abstract class into derived or based class?

    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