Abstract and Sealed Class Interview Questions in C#

Abstract and Sealed Class Interview Questions in C# with Answers

In this article, I am going to discuss the most frequently asked Abstract and Sealed Class Interview Questions in C# with Answers. Please read our previous article where we discussed the most frequently asked Interface and Inheritance Interview Questions and Answers in C#. As part of this article, we are going to discuss the following Abstract and Sealed Class Interview Questions in C# with answers.

  1. When to use Abstract Class in C#?
  2. What is an Abstract Class in C#?
  3. Can you create an instance of an abstract class?
  4. What is a Sealed Class?
  5. What is the abstract method in C#?
  6. When to use the abstract method?
  7. Can a sealed class be used as a base class?
  8. Can an abstract class have a constructor? If so what is the use?
  9. We cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?
  10. An abstract method in an abstract class does not have any implementation, so what is the use of calling it from the abstract class constructor?
  11. When should a class be declared as abstract?
  12. When should a method be declared as sealed?
  13. What is the difference between private and sealed method?
  14. When should a class be declared as sealed?
  15. What are the differences between an abstract class and a sealed class?
  16. Why should the method have an abstract keyword if it does not have the body?
  17. What are the characteristics of an abstract class?
  18. Why can the abstract class not be instantiated?
  19. Who will provide the implementation (body) for abstract methods?
  20. What type of members can we define in an abstract class?
  21. Will abstract class members are created when a subclass object is created?
  22. How can we execute static and non-static concrete members of the abstract class?
  23. Can we declare the abstract method as static?
  24. Can we declare the concrete class as abstract?
  25. Explain the differences between overriding methods and abstract methods?
  26. What is the need for abstract classes in application development?
When to use Abstract Class?

When we have a requirement where our base class should provide the default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

What is an Abstract Class?

A class that is declared by using the keyword abstract is called an abstract class. An abstract class is a partially implemented class used for developing some of the operations which are common for all next level subclasses. So it contains both abstract methods, concrete methods including variables, properties, and indexers.

It is always created as a superclass next to interface in object inheritance hierarchy for implementing common operations from the interface.

An abstract class may or may not have abstract methods. But if a class contains an abstract method then it must be declared as abstract. The abstract class cannot be instantiated directly. It’s compulsory to create/derive a new class from an abstract class in order to provide the functionality to its abstract functions.

Can you create an instance of an abstract class?

No, abstract classes are incomplete and we cannot create an instance of an abstract class.

What is a Sealed Class? 

A sealed class is a class that cannot be inherited from. That means if we have a class called Customer that is marked as sealed. No other class can inherit from the Customer class.

What is the abstract method?

A method that does not have the body is called an abstract method. It is declared with the modifier abstract. It contains only the Declaration/signature and does not contain the implementation/ body of the method. An abstract function should be terminated with a semicolon. Overriding of an abstract function is compulsory.

When to use the abstract method?

Abstract methods are usually declared where two or more subclasses are expected to fulfill a similar role in different ways.

Can a sealed class be used as a base class? 

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

Can an abstract class have a constructor? If so what is the use?

Yes, an abstract class can have a constructor. In general, a class constructor is used to initialize fields. Along the same lines, an abstract class constructor is used to initialize fields of the abstract class. We would provide a constructor for an abstract class if we want to initialize certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every child’s class. This prevents duplicate code.

We cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?

Though we cannot create an instance of an abstract class, we can create instances of the classes that are derived from the abstract class. So, when an instance of a derived class is created, the parent abstract class constructor is automatically called.

Note: Abstract classes can’t be directly instantiated. The abstract class constructor gets executed through a derived class. So, it is a good practice to use a protected access modifier with the abstract class constructor. Using public doesn’t make sense. 

An abstract method in an abstract class does not have any implementation, so what is the use of calling it from the abstract class constructor?

If we want the abstract method to be invoked automatically whenever an instance of the class that is derived from the abstract class is created, then we would call it in the constructor of the abstract class.

When should a class be declared as abstract?

A class should be declared as abstract

  1. When If it has any abstract methods
  2. If it does not provide implementation to any of the abstract methods it inherited
  3. When it does not provide implementation to any of the methods of an interface
When should a method be declared as sealed?

If we don’t want to allow subclasses to override the superclass method and to ensure that all sub-classes use the same superclass method logic then that method should be declared as sealed.

The sealed method cannot be overridden in sub-classes violation leads to a Compile-time error:

What is the difference between private and sealed method?

The private method is not inherited whereas the sealed method is inherited but cannot be overridden. So private method cannot be called from sub-classes whereas the sealed method can be called from sub-classes. The same private method can be defined in sub-class and it does not lead to Compile-time error.

When should a class be declared as sealed?

In the below situations we must define the class as sealed

  1. If we don’t want to override all the methods of our class in sub-classes.
  2. If we don’t want to extend our class functionality.
What are the differences between an abstract class and a sealed class?
Abstract and Sealed class Interview Questions in C# with Answers
Why should the method have an abstract keyword if it does not have the body?

In a class, we are allowed only to define a class with the body. Since we are changing its default property (means removing its body) it must have the abstract keyword in its prototype.

What are the characteristics of an abstract class?
  1. The abstract class can contain both abstract methods and non-abstract (concrete) methods.
  2. It can contain both static and instance variables.
  3. The abstract class cannot be instantiated but its reference can be created.
  4. If any class contains abstract methods then it must be declared by using the keyword abstract.
  5. An abstract class can contain sealed methods.
  6. The abstract method or class cannot be declared as sealed.
  7. A subclass of an abstract class can only be instantiated if it implements all of the abstract methods of its superclass. Such classes are called concrete classes to differentiate them from abstract classes.
Why can the abstract class not be instantiated?

Because it is not fully implemented in the class as its abstract methods cannot be executed. If the compiler allows us to create the object for the abstract class, then we can invoke the abstract method using that object which cannot be executed by CLR at runtime. Hence to restrict calling abstract methods, the compiler does not allow us to instantiate an abstract class.

Who will provide the implementation (body) for abstract methods?

Sub-class developers provide the body for abstract methods according to their business requirements. Basically, in projects, abstract methods (method prototype) are defined by the superclass developer and they are implemented by sub-class developers.

What type of members can we define in an abstract class?

We can define all static and non-static members including properties, fields, indexes and also abstract methods.

Will abstract class members are created when a subclass object is created?

Yes, its non-static members get memory when its concrete sub-class object is created.

How can we execute static and non-static concrete members of the abstract class?

Static members can be executed directly from its main method and its non-static members are executed by using its concrete sub-class object.

Can we declare the abstract method as static?

No, we are not allowed to declare the abstract method as static. It leads to CE: the illegal combination of modifier abstract and static. If the compiler allows us to declare it as static, it can be invoked directly which cannot be executed by CLR at runtime. Hence to restrict in calling abstract methods compiler does not allow us to declare the abstract method as static.

Can we declare the concrete class as abstract?

Yes, it is allowed. Defining a class as abstract is a way of preventing someone from instantiating a class that is supposed to be extended first. To ensure our class non-static members are only accessible via sub-class object we should declare the concrete class as abstract.

Explain the differences between overriding methods and abstract methods?

The concept of the abstract method is similar to the concept of method overriding because in method overriding if a Parent class contains any virtual methods in it, then those methods can be reimplemented under the child class by using the override modifier.

In a similar way, if a parent class contains any abstract methods in it, those abstract methods must be implemented under the child class by using the same override modifier.

The main difference between method overriding and abstract method is in case of method overriding the child class reimplementing the method is optional but in case of the abstract method, the child class implementing the method is mandatory.

What is the need for abstract classes in application development?

The concepts of abstract methods and abstract classes are an extension to the inheritance wherein inheritance we have been discussing that with the help of a parent class we can provide property to the child class that can be consumed by the child classes which gives us reusability.

Along with the parent providing property to the children, the parent can also impose the restriction on the children with the help of abstract methods so that all the child classes have to full fill the restriction without failing.

In the next article, I am going to discuss the most frequently asked Polymorphism Interview Questions in C# with Answers. Here, in this article, I try to explain the most frequently asked Abstract and Sealed Class Interview Questions in C# with Answers. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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