Types of classes avalable in C#:
1.Instance class
2.Abstract class
3.Sealed class
4.Static class
5.partial class
6.nested class
1.Instance class:
Instance class is a class on which we are allowed to create many instances or object.
*An instance class can act as a parent class as well as child class
*When we allow instance on a class ,there is a possability of increase in the memory consumption.
*Allowed depth of inheritance in the child class is 4.
2.Abstract class:
Abstract class is class which contain 0 or more abstract member( methods,properties
,indexer,event)
.
* Abstract is an incomlet class.
*We must give implimention to the abstract member in your derived class.
*If u are not giving implimention to the abstract member in your derived class then you should declare your derived class as method.
*We can not be able to create an object of an abstract class(since it incomplet).
*We can create a variable for the abstract class.
Note:
you need to create as abstract class when you have family of related class which shaire some common bahaviour and having some uncommon bahaviour.
->Understanding abstract class and interface become very important when we are writting the Framework.
Sample code:
3.Sealed class:
A sealed class is a class which can not act as a base class for any other class.
*sealed class can never be a parent class
*all the members declared in the sealed class become sealed member By default.
Q).when u declare ur class as sealed calss?
Ans:When u think that ur class is having fully funactionly and if u do'nt want to allow any ane to override
Q).Can we declare a vartual or abstract method in a sealed class?
Ans: NO, bec virtual and abstract member bahaviour is allowed to be modified in the derived class.
Sample code:
public partial class sealed_class : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
A a1 = new A();
int r1 = a1.m1(10, 20);
Response.Write(r1);
}
}
public sealed class A
{
public int m1(int x, int y)
{
return x + y;
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
A a1 = new A();
int r1 = a1.m1(10, 20);
Response.Write(r1);
}
}
public sealed class A
{
public int m1(int x, int y)
{
return x + y;
}
}
4.Static class:
Static class is a class in which we can only create static member But in case instance class ,we can create both instance nember as well as static member.
*A static class not act as Base class as Derived.
*static class can not participate in inheritance.
Note:
if u supposed declare the class as static class then no need to create object for accessing varibele but we can access variable and method ussing class name
Sample code:
public partial class Static_class : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int r1 = A.x;
int r2 = A.m(10, 20);
Response.Write(r2);
}
}
public static class A
{
public static int x = 10;
public static int m(int x, int y)
{
return x + y;
}
}
Note: in this class without object we are accessing variable bec only for static class
{
protected void Page_Load(object sender, EventArgs e)
{
int r1 = A.x;
int r2 = A.m(10, 20);
Response.Write(r2);
}
}
public static class A
{
public static int x = 10;
public static int m(int x, int y)
{
return x + y;
}
}
Note: in this class without object we are accessing variable bec only for static class
5.partial class:
Partial class is useful when the class functionality is
too big (i.e when number of lines of code in the class is too big) and when a
developer want to share some of the functionalities with other developers this
is useful.
6.Nested class:
A class define as another class call as nested class.
-> A type define as another type call as nested type.
->A Struct define as another struct call as nested struct.
->32 level of nesting class is allowed in a class .
->A Struct define as another struct call as nested struct.
->32 level of nesting class is allowed in a class .
Sample code:
public partial class Nested_class : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
D d = new D();
int r2 = d.x;
int r3=d.m1();
Response.Write(r2);
Response.Write(r3);
D.E e1 = new D.E();
int r4 = e1.y;
int r5 = e1.m2();
Response.Write(r4);
Response.Write(r5);
}
}
public class D
{
public int x = 10;
public int m1()
{
return 20;
}
public class E
{
public int y = 30;
public int m2()
{
return 40;
}
}
}
}
public class D
{
public int x = 10;
public int m1()
{
return 20;
}
public class E
{
public int y = 30;
public int m2()
{
return 40;
}
}
}
No comments:
Post a Comment