Tech Point Fundamentals

Tuesday, October 20, 2020

Sealed Class Interview Questions C#

Sealed Class Interview Questions And Answers C#

SealedClass-InterviewQuestionsAndAnswers
  A sealed class is the most frequent topic for the interview today. Today, we will discuss the most common interview questions and answers based on the sealed class in C# for experienced developers.




Introduction

In C# sealed keyword applies restrictions on the class, method, property, indexer, or event. If you create a sealed class, it cannot be derived. If you create a sealed method, it cannot be overridden. In C#, we can use a sealed keyword before or after the access modifier to define the class as sealed classes.


Please read more about the sealed class and sealed methods here




01. What are the different ways to prevent inheritance in C#?

Normally we use the "sealed" modifier to prevent the inheritance in C#. In VB.Net "NotInheritable" keyword serves the purpose of sealing. In Java "final" keyword serves the same purpose. 


But based on the requirement, we can use the following in C#:

i) Private Constructor: If you define a default parameterless private constructor in any class, then it cannot be inherited or instantiated due to its protection level. But if there is any public constructor along with the default parameterless private constructor, then it can be instantiated, but still cannot be inherited.

Please read more about the private constructor here. You can also watch the private constructor video here.

ii) Static Class: Since all the static classes are sealed implicitly, you can also use static class based on the requirement.

Please read more about the static class hereYou can also watch the static class video here.

iii) Structure: Since all the structs are sealed implicitly.  A struct cannot inherit from any struct or class but a struct can implement the interface. We can also use struct based on our requirements. In the .Net Framework, structs are used for defining a lot of namespaces and libraries.

Please read more about the structure here.



02. What is a sealed class in C#?

A class modified by the "sealed" keyword is known as a sealed class. Sealed classes are used to restrict the inheritance feature of object-oriented programming.

Sealed classes restrict the users from inheriting the class. The sealed keyword tells the compiler that the class cannot be extended. 

You can watch the sealed class video here.



03. What is a final class?

In C# we use the "sealed" keyword for making a class final. The "final" keyword is used in Java for sealing.



04. How can you define a sealed class in C#?

A class can be marked as sealed by the "sealed" keyword in C#.  The sealed keyword can be used either after or before the access modifier of the class.        

Please read more about the sealed class here.


05. Can an interface be defined as sealed in C#?

No, an interface cannot be defined as sealed, but in C# 8 interface can have sealed methods. 

Please read more about the sealed interface method here.




06. Can a struct be defined as sealed in C#?

No, a struct cannot be marked as sealed explicitly because it is sealed by default.

07. Can a sealed class be inherited by any class in C#?

No, a sealed class cannot be inherited by any class or struct.


08. Can a sealed class inherits from a static class in C#?

No, a sealed class cannot inherit from any static class because a static class cannot be inherited since it is sealed implicitly.

09. Can a sealed class be used as a base class in C#?

No, a sealed class cannot be used as a base class in C#.

10. A sealed class can be instantiated or not in C#?

Yes, a sealed class can be instantiated like normal classes using the "new" keyword.



11. Can a sealed class define constructors in C#?

Yes, a sealed class can define constructors in C#. It can also have a static constructor.

12. Can a non-child or stand-alone class can be defined as sealed in C#?

Yes, a non-child class can also be marked as sealed in C#.

13. Can a sealed class be abstract in C#?

No, a sealed class cannot be abstract.

14. Can a sealed class be marked as static in C#?

No, a sealed class cannot be static because static classes are sealed by default.

15. Can a sealed class define a private constructor in C#?

Yes, a sealed class can define private constructorsIf you define a private constructor in a sealed class it cannot be instantiated until unless there is a public constructor. A sealed class having a private constructor is used for singleton implementation. 



16. Can a constructor be defined as sealed in C#?

No, a constructor cannot be marked as sealed.

17. Is it allowed to define destructor in a sealed class?

Yes, a sealed class can have a destructor in C#. 

18. Can a sealed class implement interface in C#?

Yes, a sealed class can implement an interface. But remember a static class is also sealed implicitly, but it cannot implement any interface.

19. Can a sealed class have abstract methods in C#?

No, an abstract method can only be declared in an abstract class but an abstract method can be overridden in the sealed class.

20. Can a sealed class define virtual methods in C#?

No, a virtual method cannot be defined in a sealed class.



21. Can a sealed class define sealed methods in C#?

Yes, a sealed method can be defined in a sealed class, but the sealed class must be a derived class, and the base class must have virtual or abstract methods for override.

22. Can methods defined inside a sealed class be inherited in C#?

No, since a sealed class cannot be inherited, the methods defined inside the sealed class cannot be inherited.


23. Can a partial class be sealed in C#?

Yes, a partial class can be defined as sealed in C#.

Please read more about the partial class here.


24. Can a sealed class be private or a sealed class with all members as private is allowed in C#?

No, a sealed class cannot be declared private explicitly. In fact, any class defined in a namespace cannot be explicitly declared as private, protected, protected internal, or private protected.


Yes, a sealed class with all members as private is allowed in C#.

25. When should a class be declared as sealed?

Sealing is often used to encapsulate a logic that needs to be used across the application but without any alteration to it. sealed class is used to define the inheritance level of a class. You can restrict a class from inheritance for security reasons in C# by declaring it a sealed class.



26. Can a "sealed" keyword be used in VB.Net also for sealing a class?

No, in VB.Net "NotInheritable" keyword is used for the purpose of sealing a class. For sealing a property "NonOverrideable" keyword is used.


27. What is the difference between abstract class and sealed class in C#?

    1. An abstract class can only be used as a base class, while a sealed class cannot be used as a base class.
    2. An abstract class contains abstract methods but a sealed class cannot contain any abstract method.
    3. An abstract class contains virtual methods but a sealed class cannot contain any virtual method.
    4. An abstract class cannot be instantiated, but a sealed class can be instantiated.
    5. An abstract class cannot be the bottom-most level in the inheritance hierarchy but a sealed class may be the bottom-most.

Please read more about the abstract class vs interface hereYou can also watch the abstract class vs interface video here.


28. How a sealed class is different from a static class in C#?

Although both a sealed class and a static class cannot be inherited by any class or struct but following are the key difference between them:


    1. A static class cannot be instantiated while a sealed class can be instantiated.
    2. A static class cannot define any indexer while a sealed class can define the indexers.
    3. A static class cannot have any destructor while a sealed class can have a destructor.
    4. Static class cannot have any instance constructor while a sealed class can have both static and instance constructors.
    5. A static class cannot inherit from any class or interface, but a sealed class can inherit from a class or interface.
    6. A static class can only contain static members, but a sealed class can have both static and non-static members.
    7. Members of a static class are accessed by the class name itself while sealed class members can only be accessed by the object of the class.
    8. Only a single global instance is created by the runtime which remains in the memory for the lifetime of the app domain but, it is not true for the sealed class.

Please read more about the static classes hereYou can also watch the static class video here.

29. How a sealed class is different from a class having a private constructor in C#?

A sealed class cannot be inherited but it can be instantiated. On the other hand, a class having a private constructor neither can be inherited nor instantiated due to its protection level.

But if there is any public constructor along with the default parameterless private constructor, then it can be instantiated, but still cannot be inherited.



30. What is the difference between sealed class and singleton class in C#?

A sealed class cannot be inherited but it can be instantiated in C#. On the other hand, a singleton is a design pattern that ensures that only one instance of a particular class is available at any given point of the time for the entire application.

Normally all the singleton classes are sealed, to restrict the inheritance. Because we know that the singleton class private constructor cannot prevent from inheriting in any nested class within the singleton class. 

31. Can you give me some sealed class examples available in the .Net Framework?

    1. System.String class is a sealed class.
    2. All the structs are sealed implicitly.
    3. All the enums are also sealed implicitly.
    4. All the value types are sealed.
    5. All the static classes are sealed implicitly.

32.  The class containing the Main() method can be sealed or not in C #?

The class containing the main method() can be sealed in C#. The static Main() method is used as the entry point for the Console Application and Windows Application.


33.  How a sealed class can solve the "fragile class problem" in C #?

A sealed class can solve the fragile class problem in C# by preventing inheritance.


Please watch more about the fragile base class solutions here.





No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.