Class and Object in C#
Class :
v Class
is a collection of variables, constants, methods, properties and constructors
etc. These all are called members of
class.
v class
keyword in used to create a class in
C#.
v Classes
are reference types that hold the object created dynamically in a heap.
v Classes
are the user defined data types that represent the state and behaviour of an
object. State means properties or data and behaviour means functionality that
objects can perform.
v The
default access modifier of a class is Internal.
v The
default access modifier of class members is Private.
v To
access the class members, you use the dot (.) operator.
v The
dot operator links the name of an object with the name of a member.
The syntax of a class definition is
given as follows:
AccessSpecifier class NameOfClass
{
Class members like variables/ functions/methods/properties etc.
}
For example:
public class Students
{
int id=100;
string name;
}
Here,
public - access specifier/modifier.
class - reserved keyword.
Students – User define class name.
Id, name – variables.
100 = constant.
Object:
v In
C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop
etc.
v Object
is an entity that has state and behaviour. Here, state means data and behaviour
means functionality.
v Object
is a runtime entity, it is created at runtime.
v A
program may create many objects of the same class.
v Objects
are also called instances, and they can be stored in
either a named variable or in an array or collection.
v Object
is an instance of a class. All the members of the class can be accessed through
object.
v In
C#, an object of a class can be created using the new keyword
and assign that object to a variable of a class type.
Example: Create an Object of a Class
Student myStudent = new
Student();
Here we have created an
instance of class “Student” and we have defined “myStudent” variable as a
reference object and The new keyword allocates memory at runtime.
You can now access public
members of a class using the object.MemberName notation.
Example: Access Members of a Class
Student mystudent = new
Student();
mystudent.FirstName =
"Steve";
mystudent.LastName = "Jobs";
You can create multiple
objects of a class with different values of properties and fields.
Example: Create Multiple Objects of a Class
Student mystudent1 = new
Student();
mystudent1.FirstName =
"Steve";
mystudent1.LastName =
"Jobs";
Student mystudent2 = new
Student();
mystudent2.FirstName =
"Bill";
mystudent2.LastName =
"Gates";
Advantages of Objects and Classes:
1.
Objects and classes help us
to divide a large project into smaller sub-problems.
2.
This helps to manage
complexity as well as make our code reusable.
Suppose you want to create a game that
has hundreds of enemies and each of them has fields like health, and methods
like shoot () and run ().
With C# we can create a single Enemy
class with required fields and methods. Then, we can create multiple enemy
objects from it.
Each of the enemy objects will have its
own version of health and ammo fields. And, they can use the common shoot() and
run() methods.
Now, instead of thinking of projects in
terms of variables and methods, we can think of them in terms of objects.
Real Life Examples:
v Class
is the blueprint for the object. Class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows, etc. We can build a
house based on these descriptions. House is the object.
v Like
many houses can be made from the sketch, we can create many objects from a
class.
v If
animal is the class then dog, tiger, lion and elephant etc. is the object.
v If
human is the class then man and women is the object.
v A
dog has legs and eyes, then eyes is the variable in the technical concept, this
is the property and the dog may run or may walk, these are methods.
v A
car is an object. The car has attributes, such as weight and colour, and
methods, such as drive and break.
Comments
Post a Comment