Object-Oriented Programming (OOP)¶
This is most likely the most confusing and important part of C#. To clear up any confusion about OOP, think of it like this:
"An object is an instance of a user-defined data type that also holds the procedures (methods) that perform on it's data."
Before diving deeper into OOP, let's compare it to the opposite approach: Procedural Programming
.
Procedural Programming | Object-Oriented Programming |
---|---|
Focuses on functions (procedures) that operate on data. | Focuses on objects, which contain both data and behavior. |
Data is separate from logic. | Data and logic are bundled together in objects. |
Example: C, older programming styles. | Example: C#, Java, Python (OOP languages). |
Less structured for large projects. | Easier to scale and maintain. |
Example of procedural programming:
string name = "Alice";
void SayHello(string name)
{
Console.WriteLine("Hello, " + name);
}
SayHello(name); // Function call
Example of OOP:
Why OOP?¶
- Faster and easier to execute
- Clear structure
- Prevents DRY "Don't Repeat Yourself"
- Easier to maintain, scale, modify and debug
- Provides modularity by making the code re-useable
The four pillars of OOP¶
1. Encapsulation: keep data safe inside of objects.¶
Restricting direct access
to some of the object's data. This prevents accidental modification
and enforces controlled access
.
As you can see abstraction is mostly done by using properties
:
2. Abstraction: hide unnecessary details¶
Hiding the unnecessary details from the user and exposing only what's essential
.
As you can see abstraction is mostly done by using access modifiers
:
Modifier | Description |
---|---|
public |
The code is accessible for all classes. |
private |
The code is only accessible within the same class. |
protected |
The code is accessible within the same class, or in a class that is inherited from that class. |
internal |
The code is only accessible within its own assembly, but not from another assembly. |
There's also two combinations: protected internal
and private protected
.
3. Inheritance: reuse and extend existing code¶
Allows a class (child
) to inherit attributes and behavior
from another class (parent
), reducing code duplication.
4. Polymorphism: one interface, many implementations¶
The same method can have different behaviors depending on the object using it.
Another example of polymorphism are interfaces which are completely abstract classes
that only contain method signatures
. It's more of a contract showing what the object should be about, how you implement them is up to you.
Interfaces should always start with I, it's an unspoken rule.