Thursday, June 2, 2016

OOP Concepts

Encapsulation is the process of keeping one or more items within a single package. it prevents access to implementation details. access specifiers are public, private, protected, internal etc.

E.g.
public can be accessed any code.

private can only be accessed by code in the same class or struct.

protected can only be accessed by code in the same class or struct, or in a derived class.

internal can be accessed by any code in the same assembly only.

protected internal can be accessed by any code in the same assembly, or by any derived class in another assembly.

Abstraction is the process of providing only essential information to the outside real world and hiding overall background details to present an object. It relies on the separation of interface and implementation.to hide irrelevant details 
For example, we continue with “Bike” as an example, we have no access to the piston directly, we can usestart button to run the piston. Just imagine if a bike manufacturer allows direct access to piston, it would be very difficult to control actions on the piston. That’s the reason why a bike provider separates its internal implementation from its external interface.

private string GetEngineMakeFormula(){ return formula;}

public string DisplayMakeFormula() { //"GetEngineMakeFormula()" is private but accessible and limited to this class only
return GetEngineMakeFormula();}

Polymorphism means having many forms. Generally, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. (Overloading)
Vehicle airplane = new Airplane();
A Bike can be into two forms like cell start or kick start. We can later on decide which form or method we will use to start bike to go for drive

Inherritance

Initialize Child will Run Parent Constructor 1st only Child Constructor.
Child can Run Parent Method.
Can initialize Parent Object.
Override only for virtual or abstract method.

Constructor no return value, can overloaded

Destructor use for manual cleanup to destroy the objects that we no longer want to use.
~Bike(){ //Destructor }

No comments:

Post a Comment