OOPs Pillars
OOPs Pillars
Advantages:
OOP is faster and easier to execute | Increases the flexibility and maintainability.
OOP provides a clear structure for the programs
OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the
code easier to maintain, modify and debug.
OOP makes it possible to create full reusable applications with less code and
shorter development time
OOPS
Abstraction:
A mechanism to provide the details about essential features without describing the
background details.
Ex: During Login process, User enters the UserId & Psw which is the essential ,
information for login, Here you don’t know about the background details(How the
UserID & psw gets verified).
Ex: In a application, The Admin user has a different level of abstraction as compared to
normal users.
Encapsulation:
A mechanism of binding the object state(variables/fields) and behaviour(methods)
together into a single unit.
The encapsulation is mainly achieved by creating a class.
Class is a kind of container, Which encapsulates set of fields and methods.
Fields
Class
Inheritance:
Methods
A mechanism of acquiring/ accessing/ consuming the members of a class by
another class.
The class whose members are inherited is called Base class/ Parent class, The
class who inherits is called as derived class/ Child class.
Reduces code redundancy, Improves reusability.
Class A
(Parent Class)
Class B
(Child Class)
Types of Inheritance:
Types of
Single Level Hybrid
Inheritance Inheritance Inheritance
Class A Class A
Class A
Polymorphism:
Ability of an object to behave in multiple ways.
Ex: Smart phone is a single object but can be used for multiple purposes, Music call etc
Polymorphism
Static Dynamic
Polymorphism Polymorphism
Static Polymorphism Compile time Polymorphism– Method Overloading & Operator
Overloading.
Dynamic Polymorphism/Runtime polymorphism- Method Overriding.
Class:
A class is a container which contains data members (like fields, properties) and
member functions(like methods, constructor, destructors). It is a user-defined
data type.
Class is a reference type, acts as a template for an object.
class combines the fields and methods(member function which defines actions)
into a single unit. In C#.
Object:
An object is an identifiable entity with some characteristics and behaviour, An object is
an instance of a class. When a class is defined no memory is allocated but when it is
instantiated (i.e. object is created) The memory is allocated.
Access Modifiers:
Used to specify the accessibility of a member(field , Methods) or a type(Class ,
Inheritance).
Supports abstraction by exposing only necessary members and hiding other members.
Public(From everywhere)
Private(inside own class)
Protected (inside own class + Derived class)
Internal(Default) (Own assembly)
Protected-internal( Own assembly + Derived class outer assembly)
MODIFIERS SAME DIFFERE ASSEMB
ASSEMBLY
NT LY
Same Derived Otherclas DerivedClass OtherClass
Class class s
PRIVATE Yes No No No No
PROTECTED Yes Yes No Yes No
INTERNAL Yes Yes Yes No No
PROTECTED Yes Yes Yes Yes No
INTERNAL
PUBLIC YES YES YES YES YES
Constructor:
It’s a special type of function which has same name as it’s class.
No return type not even VOID.
Supports Overloading with parameters(Multiple signature function)
Invoked automatically where object is created, Responsible for object
initialization and memory allocation.
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
Default Constructor:
The default constructor has no parameters.
When the class has no constructor, default constructor is served by the compiler
to that class.
Used to assign default values to instance variables of the class.
Parameterized Constructor:
The Parameterized Constructor has one of more parameters.
Used to assign default values to instance variables of the class.
Basically the values for the var will be passed during obj initialization, So the
values will be assigned.
Static Constructor:
A special type of constructor that gets called before the first object of class is
created.
Used to initialize any static fields , or to perform a particular action that needs to
perform only once.
A class can have only one static constructor and it must be It does not take
access modifiers and having no access modifier.
Private Constructor:
Restricts a class external instantiation, Outside the class obj creation is not
permitted. But in nested class you can create the obj.
Used to implement singleton pattern(Singleton class only has one object
initialized).
A class can have multiple Private and Public constructors.
Destructor:
A special type of method which has the same name as it’s class name preceded
by a tilde(~) sign.
Used for releasing the memory for the created object.
It’s automatically called before an object is destroyed, Can’t be called explicitly.
A class can have only one destructor.
Method:
A method is a collection of statements that perform some specific task and
return the result to the caller.
A method has name, parameters(separated by comma), body(wrapped using
curly braces{} ) and a return type.
Methods are declared in a class or struct by specifying the access modifiers like
as public or private.
Method Overloading:
In method overloading, a class has more than one method with same name but
having different signature(Number of parameter and type of parameter).
Adds or extends the behaviour of an existing method.
An example of compile time polymorphism since actual method calling is
resolved at compile time.
Method Overriding:
In method overriding, a derived class have the same methods with exactly the
same signature as base class.
Change the behaviour of an existing method in the base class.
Unlike method overloading, here methods must have same access modifiers.
Private access modifier is not allowed( Though the scope of private is not out of
classs)
An example of runtime polymorphism, Since actual method calling is resolved at
runtime.
In base class method Virtual keyword should be mentioned, So it can be
overridden.
Ex:
Class A
{
Public virtual int Random( )
{
return a ;
}
}
Class B : A
{
Public override int Random( )
{
return B;
}
}
Method Hiding:
A way to hide a method of base class into derived class using new keyword.
Unlike method overriding, the base class method you don’t need to declare as
virtual.
Unlike method overriding, the actual method calling is resolved at Compile time.
Ex:
Class A
{
Public int Random( )
{
return a ;
}
}
Class B : A
{
Public new int Random( )
{
return B;
}
}