Introduction
Class
Class can be defined as a collection of similar kind of objects.
Class can be defined by various ways:
- Conceptually
- Programmatically
Conceptually
- It is a classification.
- Customization - creates according to our need.
- Aggregation - for various object.
Programmatically
- Logical entity - because it doesn't takes any memory.
- Type for Object - We can make object of class types.
- Class is a keyword.
- Class consists - member function & member variable.
- Class keyword knows only two operators
--Assignment operator (=)
--Ampersand (&)
Class is a Template because through this we can makes many
objects.Class are also predefined.
Syntax of Class
class
ClassName
{
// member variables ...
// member function ...
}
In C#, we have many types of
classes
- Abstract class
- Partial class
- Sealed class
- Static class
Abstract class
- Abstract class is one of which we can't create a direct object.
- We can't instantiate an abstract class.
- It consists both concrete or abstract or both method in it.
- Abstract keyword is for making a class Abstract.
- Used for Memory managenemt.
- Abstract classes are the alternatives of interfaces.
Program of Abstract class
using
System;
namespace
abstractDeep
{
abstract class
Car
{
string regno;
int modelno;
public int
Model // This is property 1st
{
set { modelno =
value; }
get { return
modelno; }
}
public string
Resgistration // This is property 2nd
{
set { regno =
value; }
get { return
regno; }
}
public void
brakes() // function
{
Console.WriteLine("Brakes
are important to drive safely");
}
}
class Maruti:Car
{
public static
void Main()
{
Maruti mr =
new Maruti();
Console.WriteLine("
Maruti CAR DESCRIPTION");
Console.WriteLine("*******************************************");
mr.Model = 2005;
mr.Resgistration = "UP-14 Z 6224-DEEPAK";
Console.WriteLine("CarModel
: " + mr.Model + "");
Console.WriteLine("CarRegistration
: " + mr.Resgistration + "");
mr.brakes();
Console.WriteLine("*******************************************");
Console.Read();
}
}
}
Output
Scenario of Abstract class
If we have to make Cars with some same features then, we make the abstract
class then inherit this class with other class of car which can company name
(maruti,indica,Tata etc.).Here we can't make the object of Car class but we can
make object of maruti cars like Wagon, maruti 800.
Partial class
A class which is distributed over no. of files known as partial class. When
we want to create a single class distributed over a number of files then
we use the concept of partial class.
partial - keyword is used for making the class partial.
Syntax of Partial class
public
partial class
Student
{
public void
DoHomeWork()
{
}
}
public
partial class
Student
{
public void
GoToLunch()
{
}
}
Scenario of Partial class
It is helpful in case when we make a class with many function then no. of
person are assigned for that work and they make partial class with same name.
After compilation they can be combined into a single .dll (dynamic link
library).
Sealed class
- It is also called LEAF CLASS.
- Means which can't be inherited or which can't have child class.
- We can only create objects of sealed class instead of inherit it.
Program of Sealed class
using
System;
namespace
Sealednmsp
{
class Car
{
string regno;
int modelno;
public int
Model // This is property 1st
{
set { modelno =
value; }
get { return
modelno; }
}
public string
Resgistration // This is property 2nd
{
set { regno =
value; }
get { return
regno; }
}
}
sealed class
WagonR:Car
// WagonR is sealed class so u cant inherit class
WagonR further
{
public static
void Main()
{
WagonR mr =
new WagonR();
Console.WriteLine("
WagonR CAR DESCRIPTION");
Console.WriteLine("****************************************");
mr.Model = 2005;
mr.Resgistration ="UP-14 Z
6224-GHAZIABAD";
Console.WriteLine("CarModel
: " + mr.Model + "");
Console.WriteLine("CarRegistration:
" + mr.Resgistration + "");
Console.WriteLine("****************************************");
Console.Read();
}
}
class
Final : WagonR
//Error 1 'Sealednmsp.Final': cannot derive from sealed type
'Sealednmsp.WagonR'
{
}
}
Scenario of Sealed class
If we have a car wagon R ,we must make it sealed so that no one can inherit
it to use according to his need. One can only have object of Wagon R cars.
Static class
- A static class is basically the same
as a non-static class, but there is one difference: a static class cannot be
instantiated.
- Do not have a callable constructor.
- Static class position in memory is therefore fixed in one place.
- Static classes are sealed and therefore cannot be inherited.
Syntax of Static class
//
static class
static
class CollegeStu
{
public static
void DoSomething()
{
Console.WriteLine("Be
prepare for the exam");
}
public static
void DoSomethingExtra()
{
Console.WriteLine("Being
a topper of college need to do hard work with smartness");
}
}