Вы находитесь на странице: 1из 8

Classes in C#.

Net

Types of classes in C#.Net:

• Abstract Class (Virtual Class)

• Partial Class

• Sealed Class

• Static Class

Abstract Class:
An Abstract Class means that, no object of this class can be instantiated, but can make
derivation of this. It can serve the purpose of base class only as no object of this class can be created.
Abstract Class is denoted by the keyword abstract.
It is important here to note that abstract classes can have non-abstract method(s), even can have
only non-abstract method(s).

Partial Class:
This special type of class called "Partial Class" is introduced with .Net Framework 2.0. Partial
Class allows its members – method, properties, and events – to be divided into multiple source files
(.cs). At compile time these files get combined into a single class.
Partial Class is denoted by the keyword partial.
Some do's and don'ts about partial class:-
• All the parts of a partial class must be prefixed with the keyword partial.
• Accessibility, signature etc. must be same in all parts of the partial class.
• You cannot sealed one part of the partial class. In that case entire class in sealed.
• If you define any part of the partial class abstract, entire class will become abstract.
• Inheritance cannot be applied to a part of partial class. If you do so, it applies to entire class.

Sealed Class:
A sealed class is a class which cannot be inherited. A sealed class cannot be a base class. The
modifier abstract cannot be applied to a sealed class. By default, struct (structure) is sealed. It is the
last class in hierarchy. To access the members of a sealed class, you must create objects of that
class. Sealed Class is denoted by the keyword sealed.

Static Class:
A Static Class is one which cannot be instantiated or inherited. The keyword new cannot be
used with static classes as members of such class can be called directly by using the class name itself.
Following are the main characteristics of a static class:-
• A Static Class can only have static members.
• A Static Class cannot be instantiated.
• A Static Class is sealed, so cannot be inherited.
• A Static Class cannot have a constructor (except static constructor).
• Static Class is denoted by the keyword static.
For Reading.

Definition - What does Polymorphism mean?

Polymorphism is an object oriented programming concept to refer to the ability of a variable,


function, or object to take on multiple forms. A language that features polymorphism allows
developers to program in the general rather than program in the specific.

In a programming language that exhibits polymorphism, objects of classes belonging to the


same hierarchical tree (i.e. inherit from a common base class) and may possess functions bearing
the same name but each having different behaviors.

As an example, let us assume you have a base class named Animals from which the
subclasses Horse, Fish and Bird are derived. Let us also assume that the Animals class has a
function named Move, which is inherited by all subclasses mentioned. With polymorphism, each
subclass may have its own way of implementing the function. So, for example, when the Move
function is called in an object of the Horse class, the function might respond by displaying trotting on
the screen. On the other hand, when the same function is called in an object of the Fish class,
swimming might be displayed on the screen. In the case of a Bird object, it may be flying.

In effect, polymorphism trims down the work of the developer because he can now create a
sort of general class with all the attributes and behaviors that he envisions for it. When the time
comes for the developer to create more specific subclasses with certain unique attributes and
behaviors, the developer can simply alter code in the specific portions where the behaviors will differ.
All other portions of the code can be left as is.

___ __ _ _ _ __ _ __________________________________________________________

What is Polymorphism
Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms
hence the name polymorphism. Polymorphism also refered to as one name many forms or having one name with
multiple functionality.
In simple words you can use same method name with different signature or same signature but in different class. So
depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.

What are types of Polymorphism


There are basically two types of polymorphism in c# i.e.

Static Polymorphism
Dynamic Polymorphism
Static Polymorphism
Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are
overloaded with same name but having different signatures. So it is called as method overloading.

Example of Static Polymorphism


01 public class clsCalculation
02 {
03
04 public int Add(int a, int b)
05 {
06 return a + b;
07 }
08
09 public double Add(int z, int x, int c)
10 {
11 return z + x + c;
12 }
13
14 }

In above code we have a class "clsCalculation" having two functions with same name "Add" but having different
input parameters.
First function is with 2 parameters and second function having 3 parameters. So this type of polymorphism is also
known as method overloading.
It is a compile time polymorphism because compiler already knows what type object it is linking to, what are the
methods it is going to call it. So linking a method during a compile time also called as Early binding.

Dynamic Polymorphism

Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the
same name, same signature but different in the implementation.
In Dynamic polymorphism methods are overridden so it also called as method overriding.

During run time, Method overriding can be achieved by using inheritance principle and using "virtual" and
"override" keyword. so this type of polymorphism can also be called as late binding.
In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the
run time. so it is called as run time polymorphism.

Example of Dynamic Polymorphism


01 public class clsShape
02 {
03 public int _radius = 5;
04
05 public virtual double getArea()
06 {
07 return 0;
08 }
09
10 }
11
12 public class clsCircle : clsShape
13 {
14
15 public override double getArea()
16 {
17 return 3.14 * _radius * _radius;
18 }
19
20 }
21
22 public class clsSphere : clsShape
23 {
24
25 public override double getArea()
26 {
27 return 4 * 3.14 * _radius * _radius;
28 }
29
30 }

As you can see from above code that we have one base class "clsShape" with a "virtual" method "getArea()" and
two derived classes "clsCircle" and "clsShape" each having an "override" method "getArea()" with different
implementations.

Click here to know more How to Implement Inheritance in C#.


So that you have understood about Polymorphism, Static Polymorphism and Dynamic Polymorphism. Now let's see a
simple example of polymorphism.
Simple example of polymorphism using c sharp

In this example we will illustrate an example of dynamic polymorphism.we will take up above the example and try to
implement it.

01 public class clsShape


02 {
03 public int _radius = 5;
04
05 public virtual double getArea()
06 {
07 return 0;
08 }
09
10 }
11
12 public class clsCircle : clsShape
13 {
14
15 public override double getArea()
16 {
17 return 3.14 * _radius * _radius;
18 }
19
20 }
21
22 public class clsSphere : clsShape
23 {
24
25 public override double getArea()
26 {
27 return 4 * 3.14 * _radius * _radius;
28 }
29
30 }

So as you know that we have one base class "clsShape" with a "virtual" method "getArea()" and two derived
classes "clsCircle" and "clsShape" each having an "override" method "getArea()" but with different
implementation.
Now to see the output we will create "Console Application" and try to see the result.
Step 1 - Creating Console Application
1 class Program
2{
3 static void Main(string[] args)
4 {
5
6 //code goes here
7 }
8}

Step 2 - Creating Base Class Object


In this step we will create base class object and assign it to derived classes.
01 class Program
02 {
03 static void Main(string[] args)
04 {
05
06 clsShape objShape1 = new clsCircle();
07 clsShape objShape2 = new clsSphere();
08
09 }
10 }

So as you see from above code that we have created two objects "objShape1" for "clsCircle" and "objShape2"
for "clsSphere" respectively.

Step 3 - Displaying the Result


01 class Program
02 {
03 static void Main(string[] args)
04 {
05
06 clsShape objShape1 = new clsCircle();
07 clsShape objShape2 = new clsSphere();
Console.WriteLine("Radius of a Cirle is - {0}",
08
objShape1.getArea());
Console.WriteLine("Radius of a Sphere is - {0}",
09
objShape2.getArea());
10
11 }
12 }

Step 4 - Output

Radius of a Cirle is - 78.5

Radius of a Sphere is - 314

______________________________________________________________________

Note:

In the PolymorphismExample class main method, i have created three objects- Car, Ford and Honda. All
the three objects are referred by the Car type.
Please note an important point here that A super class type can refer to a Sub class type of object but
the vice versa is not possible. The reason is that all the members of the super class are available to the subclass
using inheritance and during the compile time, the compiler tries to evaluate if the reference type we are using has
the method he is trying to access.

Вам также может понравиться