Object Oriented Programming
Object Oriented Programming
Advantages:
class combines the fields and methods(member function which defines actions)
into a single unit. In C#.
Syntax:
Class className{} =
Class Car{
Codes
Creating an object :
An object is created from a class. We have already created the class named Car,
so now we can use this to create objects.To create an object of Car, specify the
class name, followed by the object name, and use the keyword new:
Constructors:
A constructor is a special method of the class which gets automatically invoked
whenever an instance of the class is created. Like methods, a constructor also
contains the collection of instructions that are executed at the time of Object
creation. It is used to assign initial values to the data members of the same
class.
Example.
class Geek
{
.......
// Constructor
public Geek() {}
.......
Protocols of constructor:
Constructor of a class must have the same name as the class name in
which it resides.
A constructor can not be abstract, final, and Synchronized.
Within a class, you can create only one static constructor.
A constructor doesn’t have any return type, not even void.
A static constructor cannot be a parameterized constructor.
A class can have any number of constructors.
Access modifiers can be used in constructor declaration to control its
access i.e which other class can call the constructor.
Types of Constructor:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Private Constructor
5. Static Constructor