Difference between Interface, abstract class, sealed class, static class and partial class in c#? If all classes available in vb.net?
The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
A static class can only contain static Methods, Properties and Fields and what you wrote in 1), 2) and 3) applies. A sealed class is a normal class which you can make an instance of, but you cannot inherit from it.
A Sealed class is a class that cannot be inherited. A partial class is a class that can be split between 2 or more source files.
abstract class
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. Horse, Pig etc.
By making Animal abstract it is not allowed to create an Animal instance.
interface
An interface should be used to implement functionality in a class. Suppose we want a horse to be able to Jump, an interface IJumping can be created. By adding this interface to Horse, all methods in IJumping should be implemented. In IJumping itself only the declarations (e.g. StartJump and EndJump are defined), in Horse the implementations of these two methods should be added.
sealed class
By making Horse sealed, it is not possible to inherit from it, e.g. making classes like Pony or WorkHorse which you like to be inheriting from Horse.
static class
Mostly used for 'utility' functions. Suppose you need some method to calculate the average of some numbers to be used in the Horse class, but you don't want to put that in Horse since it is unrelated and it also is not related to animals, you can create a class to have this kind of methods. You don't need an instance of such a utility class.
partial class
A partial class is nothing more than splitting the file of a class into multiple smaller files. A reason to do this might be to share only part of the source code to others. If the reason is that the file gets too big, think about splitting the class in smaller classes first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With