Showing posts with label Class Modifiers. Show all posts
Showing posts with label Class Modifiers. Show all posts

Sunday, June 2, 2013

Virtual class Modifier

The Virtual modifier is used to specify that a method or property can be overridden in the derived child class. Unlike the other class modifiers which are used at the class level , the virtual modifier cannot be used at the class level, instead it is used to modify the behavior of the functions and properties of the class.

The following example defines a class with a virtual function which is overridden in the derived class.

Static class Modifiers.

When a class is defined as a static class its members can be accessed without creating an instance of the class. Static classes are useful when we want to define members which do not vary from one instance to another.

The following example creates a static class with a static variable and a function.

Partial class Modifier

Partial class modifiers are used to define a single class across multiple files, this modifier is useful when we want to write large and complex classes. Splitting the class across multiple files improves the readability of the class and improves maintainability.

In the following example we shall define a single class in 2 files by adding the modifier partial to the class, create an instance of the partial class and invoke the methods which are defined in both the files.

Sealed class Modifier

A sealed class modifiers prevents the class from getting inherited, cases marked with sealed cannot be derived by other classes, trying to do so will result in a complier error.

In the following example we try to define a sealed class and try to inherit the same from a derived class.

Abstract Class Modifier

The Abstract Class modifier is used to define base classes which can be derived by other classes. An Abstract class cannot be instantiated i.e we cannot create objects from an Abstract class, trying to do so will result in a compiler error.

Abstract class generally declare only the signature of the methods, the actual method body is coded in the derived class, however it doesn’t not stop us from adding a method body in the abstract class, we can still go ahead and do some code to the method body in the base abstract class.

The following example creates a simple abstract class, and a derived class which inherits from the Abstract class.

New Class Modifier

The new class modifier is used in derived classes when the derived class declared a member with the same name / signature which already exists in the base class.

When a member is declared in the derived class using the same name / signature of a member which already exists in the base class the following warning is thrown.

What are Class Modifiers

Class Modifiers are keywords which are used to add special meaning / purpose to a class. We have seen about access modifiers in the “Access Modifiers” section, access modifiers are general and can be applied to classes and class members, similarly class modifiers are special keywords which can be added to the class definition to alter the behavior of the class.

The following are the class modifiers available in C# .Net