Object Oriented Programming
Programming Paradigm
Object
Objects can be considered as real-world instances of entities like class, that have some characteristics and behaviors.
Class
A blueprint or template for creating similar types of objects. They usually have:
Attributes or Data
Behaviors or Methods
Class Relationships
"is-a" relationship
Inheritance
A mechanism in which one class inherits the properties of another class.
Single Inheritance
Multilevel Inheritance
Multiple Inheritance (Not Supported in C# due to The Diamond Problem)
Hierarchical Inheritance
Hybrid Inheritance
Polymorphism
A concept where an object behaves differently in different situations.
Compile-time Polymorphism / Static Dispatch
Function/Method/Operator Overloading a.k.a. Early Binding
With overloading you have a function with different sets of parameters. The function that is to be executed is determined using the number and type of the parameters you provide. As these are known at compile time, the compiler already determines the function to use. Because of this, it is called compile time polymorphism.
Runtime Polymorphism / Dynamic Dispatch
Method Overriding (
virtual
/abstract
) a.k.a. Late Binding
When you are overriding a virtual function of a base class in one or more derived classes and then call this function from a base class, the actual class of the underlying object is not clear at compile time. Thus it is determined only at runtime which function is executed. That is why it is called runtime polymorphism.
Abstraction
Abstraction is the process to hide the internal details and show only the functionality.
The keyword
abstract
is used before the class or method to declare the class or method as abstract.An Abstract method is a method without a body.
The implementation of an abstract method is done by a derived class.
When the derived class inherits the abstract method from the abstract class, it must override the abstract method.
Abstraction is the concealment of unnecessary program details so that the user only sees the essential attributes.
The Abstract class and Interface both are used to have abstraction.
An Abstract class can have non-abstract Methods(concrete methods) and declare variables while in case of Interface all the methods has to be abstract and there should not be any implementation.
Encapsulation
Encapsulation refers to the bundling of data/properties with the methods that operate on that data, or the restricting of direct access to some of an object's components.
Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.
Encapsulation is achieved by taking advantage of the access modifiers that include public, private protected, private, internal, and protected that control the visibility and accessibility of the members of a class.
Encapsulation is the bundling of data, including the methods that operate on that data, into a single, private unit
In this example, the
Account
class has two private members:accountNumber
andbalance
. These members are not accessible from outside the class. The class provides public methodsGetBalance()
,Deposit()
, andWithdraw()
to allow outside code to interact with the object.
Last updated