Back to: Dot Net Interview Questions and Answers
Abstract and Sealed Class Interview Questions in C#
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.
- When to use Abstract Classes in C#?
- What is an Abstract Class?
- Can you create an instance of an abstract class?
- What is a Sealed Class?
- What is the abstract method in C#?
- When to use the abstract method?
- Can a sealed class be used as a base class?
- Can an abstract class have a constructor? If so what is the use?
- We cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?
- 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?
- When should a class be declared as abstract?
- When should a method be declared as sealed?
- What is the difference between the private and sealed method?
- When should a class be declared as sealed?
- What are the differences between an abstract class and a sealed class?
- Why should the method have an abstract keyword if it does not have the body?
- What are the characteristics of an abstract class?
- Why can the abstract class not be instantiated?
- Who will provide the implementation (body) for abstract methods?
- What type of members can we define in an abstract class?
- Will abstract class members are created when a subclass object is created?
- How can we execute static and non-static concrete members of the abstract class?
- Can we declare the abstract method as static?
- Can we declare the concrete class as abstract?
- Explain the differences between overriding methods and abstract methods?
- What is the need for abstract classes in application development?
When to use Abstract Classes in C#?
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 in C#?
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 the interface in the 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
- When If it has any abstract methods
- If it does not provide implementation to any of the abstract methods it inherited
- When it does not provide implementation to any of the methods of an interface
When should a method be declared as sealed in C#?
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 the private and sealed method in C#?
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
- If we don’t want to override all the methods of our class in sub-classes.
- If we don’t want to extend our class functionality.
What are the differences between an abstract class and a sealed class in C#?
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 (which means removing its body) it must have the abstract keyword in its prototype.
What are the characteristics of an abstract class in C#?
- The abstract class can contain both abstract methods and non-abstract (concrete) methods.
- It can contain both static and instance variables.
- The abstract class cannot be instantiated but its reference can be created.
- If any class contains abstract methods then it must be declared by using the keyword abstract.
- An abstract class can contain sealed methods.
- The abstract method or class cannot be declared as sealed.
- 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 in C#?
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 re-implemented 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 the case of method overriding the child class re-implementing the method is optional but in the 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 re-usability.
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 you enjoy this Abstract and Sealed Class Interview Questions in C# with Answers article. I would like to have your feedback. Please post your feedback, question, or comments about this article.
Can we declare the concrete class as abstract?
An abstract class can contain sealed methods. >> This should be “cannot contain sealed methods.”