Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, Object-Oriented Programming(OOP) Object-Oriented Programming(OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. Learn More, Programming concepts Almost all programs consist of the same basic 'building blocks', built together in different ways to achieve a particular goal. Variables, data types, sequence, selection, and iteration are some examples of these basic concepts, which all new programmers need to learn. This tag is mainly for all such conceptual Questions. Learn More,

Difference between Abstract Class, Static Class and Sealed Class in C#?

Description:Need to get more clarity on each one (Abstract Class, Static Class and Sealed Class) of them so that I can identify when to use which one and why

Posted by: | Posted on: Mar 24, 2022

1 answers

Replies

3

Abstract Class:

-Declared with abstract keyword
-This class is primarily created as a Inheritable class. An abstract class enables other classes to inherit from this class, but forbids to instantiate. That means one can inherit from an abstract class but cannot create object of an abstract class.
-Abstract class can have abstract as well as non-abstract methods. Abstract methods are those which are not having method definition.
-Should be used when there is a IS-A relationship and no instances should be allowed to be created from that abstract class. Example: An Animal is an abstract base class where specific animals can be derived from, i.e. Tiger, Cat etc. By making Animal abstract it is not allowed to create an Animal instance.


Static Class:

-Declared with Static keyword
-Methods in Static Class are also static along with variables of the class.
-This class cannot be instantiated, i.e we cannot have objects of this class.
-To access methods of this class, you can directly use classname.methodname.
-This class cannot be inherited.
-Mostly used for 'utility' functions. Suppose you need some method to calculate the average of some numbers to be used in the Tiger class, but you don't want to put that in Tiger since it is unrelated and it also is not related to Animal, you can create a class to have this kind of methods. You don't need an instance of such a utility class.


Sealed Class:

-Declared with Sealed keyword
-This class basically seal all its variables, methods and properties. Which means no other class can inherit anything from this class or in other words, this class cannot be inherited.
-But we can instantiate this class, i.e we can have any number of objects of a sealed class.
-By making Tiger sealed, it is not possible to inherit from it, e.g. making classes like Siberian tiger or Bengal Tiger which you like to be inheriting from Tiger.


Replied by: | Replied on: Mar 25, 2022



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview