Why do we want to have class as Abstract?
Abstract
–Abstract
classes are incomplete classes, i.e., they will have combination of implemented and unimplemented methods along with data members, properties, events, delegates and indexers.
The main idea to have class asabstract
is to have onlyBase
class that can be derivable and that will have basic functionality. As I mentioned above, we can have unimplemented method inabstract
class that gives flexibility to derived classes to implement as required along with basic functionality. And also, we can’t instantiate object ofabstract
class.Abstract
class can be with/withoutabstract
methods. But if methods are declared asabstract
in a class, that class also should be declared asabstract
.
Sealed
–Sealed
classes are classes that are not inheritable. Methods also can besealed
, that is, those methods declared assealed
can’t be overridable, i.e., derived classed can’t override those methods. And normal classes can’t havesealed
method.Sealed
keyword should be declared withoverride
keyword in the derived class' method for whichbase
class will havevirtual
method.
Why do we want to have class and method as Sealed?
The reason to have class and method as sealed is to stop the inheritance at one level.
Where to use sealed?
If you think that class is not to be inherited in your design, you can use class assealed
. Butsealed
class can inherit from interface and class. If you thinkvirtual
method cannot to be inherited in derived class at one stage, we can declare a method with sealed+override combination.
By default structures aresealed
, that is the reason structures are not supporting inheritance.
Why do we want to have class and methods as static?
In your design, if you feel that a class should have a set of methods which operate only on arguments that we pass without initiating object of that class, then we can make that class asstatic
class (Example:System.Math
). Merely call those instance methods by class name.
How this class will be loaded without creating an object?
If your code accesses any ofstatic
class’s methods, then this is the responsibility of CLR (Common Language Runtime) to load this class into memory once which is the lifetime of your application.
Object ofstatic
class can’t be instantiated. Why? Sincestatic
class can’t have instance constructor.Static
class supports inheritance but other classes can’t inherit fromstatic
class, i.e.,static
classes are sealed. Classes can havestatic
methods butstatic
classes can’t have instance member and instance methods.if
, should be declared withstatic
keyword.
Static
class is useful when you implement Singleton Design pattern.
What does partial modifier indicate?
Partial key word is to declare the class as partial meant to say it splits the classes into multiple parts under the same class name in a single namespace.
Why? So that developers can implement the functionally for the same class parallely.
But all combined will give one class. Each split class can have instance variable, instance methods, properties, indexers, events and delegates.
Structures can’t have modifiers likeabstract
,sealed
, andstatic
whereas classes can have.
Both structure and class can have partial modifiers.
As I mentioned earlier, structures can have method declaration but notvirtual
andsealed
methods. Why? Since those two are essential for inheritance. Anyhow, the structure won’t support inheritance and declaring methods using those two keywords will throw compile time errors.- Classes can have explicitly parameterless constructors whereas structures can’t.
- Member variable initialization is possible in class whereas in Structures, it is not.
- It is not possible to declare destructor in structure but in class it is possible.
- Process of converting structure type into object type is called boxing and process of converting object type into structure type is called unboxing.Example:
int a=10;
Hide Copy CodeObject ob = (object) a; //Boxing a= (int) obj; //Unboxing
- The “
this
” keyword gives different meaning in structure and class. How?- In class, “
this
” keyword is classified as value type of class type within which it is used like inside instance constructor or instance method. - In structure, “
this
” keyword is classified as variable type of structure type within which it is used.
- In class, “
Thursday, October 1, 2015
abstract, sealed, static, and partial in c#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment