Class
Class is a user defined blueprint (or user defined data type) which binds data and function together. Once a class has been defined, we can create any number of objects belonging to that class. A class is thus a collection of objects of similar types.
Objects
Object may be defined as variable of type class. Object may represent a person, place, bank account or any item that the program has to handle. Program object should be chosen in such a way that it closely match with real world object.
Example:
Samsung, Nokia, LG & iphone 7 are objects of class phone.
Mercedes, Bmw & Audi are objects of class car.
Objects have three main characteristics.
- Identity
- State
- Behavior
1. Identity
Identity is the property of an object that uniquely defines an object and distinguishes it from other objects.
Phone: MAC_Address, Phone_No.
Car: Plate_no
2. State
State defines property of an object or current values of all its attributes.
Phone: Size, Color, Model_no, Price, Manufactured_date
Car: Size, Color, Speed, Gear, Price, Manufactured_date
3. Behavior
Behavior defines operation of objects or what the object does.
Phone: MakeVoiceCall, MakeVideoCall, TakePictures
Car: change gear, apply break
Encapsulation
The process of wrapping up of data and function of similar types into a single unit or group or class is called encapsulation.
Abstraction
Abstraction is the act of representing only the essential thing without including background details or hiding the complexity of program.
Example
int x, we don’t know how int will internally work but we know how to use it.
printf(“Hello World”), we don’t know how printf actually display, however we know how to use it.
Access Modifiers / Access Specifiers
Access modifiers (or access specifiers) are keywords in object-oriented languages that set the boundary for the availability of class members (data members and member functions) beyond that class. Access specifier can be either private or protected or public.
1. Public: Class member which are declare as public can be accessible from outside the class.
2. Private: Class member which are declare as private can be accessible within the class by its member function and friend function. By default member of the class are private.
3. Protected: Class member which are declare as protected can be accessible within the class by its member function and derived class.
Inheritance
Mechanism of deriving a new class from the existing one is called inheritance. Old one is called base class and new one is called derived class or sub - class.
- Single inheritance
- Multiple inheritance
- Multi-level inheritance
- Hierarchical inheritance
1. Single inheritance
If a class is derived from only one base class, then it is called single inheritance.
2. Multiple inheritance
If a class is derived from more than one base class, then it is called multiple inheritance. It is like a child inheriting the physical features of one parent and the intelligence of another. However multiple inheritance does not support in C#. To overcome this problem we use interfaces to achieve multiple class inheritance.
3. Multi-level inheritance
If a class is derived from another derived class as shown in the figure, then it is called multi-level inheritance.
4. Hierarchical inheritance
If two or more than two classes are derived from same base class as shown in the figure, then it is called hierarchical inheritance.
Polymorphism
Poly means many and morph means form. Hence polymorphism is defined as ability to take more than one form.
There are two types of polymorphism: Static polymorphism & Dynamic Polymorphism.
Static Polymorphism |
Dynamic Polymorphism |
Static Binding
Early Binding
Compile time polymorphism
|
Dynamic Binding
Late Binding
Run time ploymorphism
|
The selection of appropriate function is done at the time of compilation.
|
The selection of appropriate function is done while the program is running.
|
Static polymorphism can be implemented by using
- Function overloading
- Operator overloading
|
Dynamic polymorphism can be implemented by using
- Function overriding
|
Function overloading
Function overloading may be defined as creating a multiple number of functions in a program of same function name but with different number of parameter and parameter of different types.
Operator overloading
Operator overloading is a type of polymorphism where we give additional meaning to the existing operator like unary and binary operator.
Function overriding
When a function is defined in a base class and redefined in a derived class with same signature for different implementation is called function overriding. Here signature refers to number of parameter, type of parameter and sequence of parameter. If the signature were different it would be function overloading.
In base class if we declare method with virtual keyword then only we can override those methods in derived class using override keyword.
Function overloading |
Function overriding |
Definition
|
Definition
|
Function signature must be different.
|
Function signature must be same.
|
Function overloading is evaluated during compile time.
|
Function overriding is evaluated during runtime.
|
Overloading is done within same class or between parent child classes.
|
Overriding is done in base and derived class.
|