Topic : Type of Classes in C#: Abstract | Partial | Sealed | Static



Type of Classes:

As we know, C# is a pure Object Oriented Programming language that provides the ability to reuse existing code. To reuse existing code C# provides various types of object-oriented concepts to complete the realistic requirements of a specific business. In This Tutorial, we will Discuss the overview of Classes that are the part of C# OOP concepts. Here we will just see the overview of Classes types. but in upcoming tutorials, we will learn about these all classes in Depth.





Abstract Class:

Abstract Class is a Class that provides a Common Definition to the Sub-classes and this is the Type of Class whose object in Not Created. The Purpose of an Abstract Class is to Provide basic or Default Functionality as Well as common functionality that Multiple derived classes can Share and Override.



  • Features of Abstract Class:

    » Abstract classes are declared using the abstract keyword.


    abstract    class     Class_Name 
    {
         //Class Body:
    }

    » We cannot create an object of an abstract class.
    » An abstract class contain abstract members as well as non-abstract members.
    » An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
    » A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
    » An abstract class can be inherited from a class and one or more interfaces.
    » An Abstract class can has access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
    » An Abstract class can has instance variables (like constants and fields).
    » An abstract class can has constructors and destructor.
    » An abstract method is implicitly a virtual method.
    » Abstract properties behave like abstract methods.
    » An abstract class cannot be inherited by structures.
    » An abstract class cannot support multiple inheritance.
    » If there is at least one method abstract in a class then the class must be abstract.



  • Guidelines:

    » Don't define public constructors within abstract class. Since abstract class cannot be instantiate and constructors with public access modifiers provides visibility to the classes which can be instantiated.
    » Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.



Partial Class:

It is a type of class that allows dividing their properties, methods and events into multiple source files and at compile time these files are combined into a single class. using This concept, It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.


  • Requirements for Partial Class:

    » Use the partial keyword to split interface, class, method or structure into multiple .cs files.


    partial    class     Class_Name 
    {
         //Class Body:
    }

    » All the partial class definitions must be in the same assembly and namespace.
    » All the parts must have the same accessibility like public or private, etc.
    » If any part is declared abstract, sealed or base type then the whole class is declared of the same type.
    » Different parts can have different base types and so the final class will inherit all the base types.
    » The Partial modifier can only appear immediately before the keywords class, struct, or interface.
    » Nested partial types are allowed.


  • Advantages:

    » We can easily write Our code (for extended functionality) for a VS.NET generated class. This will allow us to write the code of our own need without messing with the system generated code.
    » More than one developer can simultaneously write the code for the class.



Sealed Class:

Sealed Class is used to prevent accidental inheritance of the class. A sealed class can be useful only if it has methods with public-level accessibility. A sealed class cannot be an abstract class as the abstract class is intended to be derived by another class that provides implementation for the abstract methods and properties.


  • Requirements for Sealed Class:

    » A Sealed class is created using the Sealed keyword.


    sealed    class     Class_Name 
    {
         //Class Body:
    }

    » Access modifiers are not applied to a sealed class.
    » To access the sealed members we must create an object of the class.



Static Class:

A static class is a class that cannot be initialized. This means We cannot create instance variables of the class. C# does not support global scope variables. One of the ways that We can use to define something similar to global scope is by using public static class with public static properties.


  • Requirements for Static Class:

    » A Static class is created using the Static keyword.


    Static    class     Class_Name 
    {
         //Class Body:
    }

    » Inside a static class only static members are allowed, in other words everything inside the static class must be static.
    » We cannot create an object of the static class.
    » A Static class cannot be inherited.
    » It allows only a static constructor to be declared.
    » The methods of the static class can be called using the class name without creating the instance.



Don't be confuse about These types of Classes, it was just an overview of Class types, in upcoming tutorials, we will explain it more using some example. so stay connected and enjoy your learning with InfoBrother.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement