Class is a collection of fields, properties and methods. It depicts real life entities and is a fundamental concept in Object Oriented Programming.
A class is a user-defined blueprint from which objects are created . A class is a user defined data type of reference type.
Lets take a example of a Car . It has different attributes like color,maximum speed,no of gears on with a built in audio player and so on.
public class Car
{
private string _color; //a variable and a field with data type string
// a variable and a property
public string Color
{
get
{
return _color;
}
set
{
_color= value;
}
public void PlayAudio()
{
//implementation logic
}
public void PlayAudio()
{
//implementation logic
}
}
Declaration Of Class:
<access specifier><keyword:class> <class name: Car>
public class car
if access identifier is not explicitly mentioned a class has internal access identifier as default while methods and properties have private access identifier.
Declaration Of Class:
<access specifier><keyword:class> <class name: Car>
public class car
if access identifier is not explicitly mentioned a class has internal access identifier as default while methods and properties have private access identifier.
Fields : field is private to the class and stores the actual data.
Properties : Properties are exposed contract which uses the underlying fields.
Properties are also called accessors as they offer a way to get and set a field in the class.
Properties can be read-only,write-only or both by declaring get{},set{} or both.
Object :
Object is an instance of a class which is created at run time. It is a memory allocation according to the class of which it is an instance of . It is used to access properties and methods in a class.
Initializing an object
An object is initialized by using the keyword new.
Car c=new Car();
here c is an object of type car.
if we split the above initialization in two lines
Car c ; // it is a declaration which states that c is a variable of type class c;
c=new Car();// now c is an object of class Car
and we can access the properties and methods of the class
c.Color=red; //assigning value to the property of the object
string color=c.Color; //getting the value of the property from the object
c.PlayAudio(); //invoking method
Properties are also called accessors as they offer a way to get and set a field in the class.
Properties can be read-only,write-only or both by declaring get{},set{} or both.
Object :
Object is an instance of a class which is created at run time. It is a memory allocation according to the class of which it is an instance of . It is used to access properties and methods in a class.
Initializing an object
An object is initialized by using the keyword new.
Car c=new Car();
here c is an object of type car.
if we split the above initialization in two lines
Car c ; // it is a declaration which states that c is a variable of type class c;
c=new Car();// now c is an object of class Car
and we can access the properties and methods of the class
c.Color=red; //assigning value to the property of the object
string color=c.Color; //getting the value of the property from the object
c.PlayAudio(); //invoking method
Comments
Post a Comment