Abstract class Sealed class Interview Questions C#.NET

Abstract class Sealed class Interview Questions and Answers C#.NET

In this article, I am going to discuss most frequently asked Abstract class sealed class Interview Questions and Answers on C#.NET. To understand the below questions and answers first have a look at the below two articles where we discussed the Abstract class and Sealed Class along with the abstract method and sealed method in details.

Abstract Class and Abstract Methods in C#

Sealed Class and Sealed Methods in C#

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 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 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 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 Declaration/signature and does not contain 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 the 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 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 thru a derived class. So, it is a good practice to use 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 sub-classes to override 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 sealed method is inherited but cannot be overridden. So private method cannot be called from sub-classes whereas 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?
SL NO ABSTRACT CLASS SEALED CLASS
1. A class which contains one or more abstract methods is known as an abstract class. A class from which it is not possible to derive a new class is known as sealed class.
2. The abstract class can contain abstract and non-abstract methods. A sealed class can contain non-abstract methods; it cannot contain abstract and virtual methods.
3. Creating a new class from an abstract class is compulsory to consume it. It is not possible to create a new class from a sealed class.
4. An abstract class cannot be instantiated directly; we need to create the object for its child classes to consume an abstract class. We should create an object for the sealed class to consume its members.
5. We need to use the keyword abstract to make any class as abstract. We need to use the keyword sealed to make any class as sealed.
6. An abstract class cannot be the bottom-most class within the inheritance hierarchy. The sealed class should be the bottommost class within the inheritance hierarchy.
Why should method have 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 abstract class not be instantiated?

Because it is not fully implemented 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 developer.

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 sub-class 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: illegal combination of modifier abstract and static.

If 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 abstract method is near 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 is 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.

If you want to learn Abstract class and Sealed Class along with the abstract method and sealed method in details in C# then please read below articles.

Abstract Class and Abstract Methods in C#

Sealed Class and Sealed Methods in C#

SUMMARY

In this article, I try to explain most frequently asked Abstract class Sealed class Interview Questions and Answers on C#.NET. I hope this article will help you with your need. 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 *