OOP stands for Object-Oriented Programming.
OOP is a programming model in computer science that relies on the concept of Classes and Objects.
It is used to structure a software program into a simple and reusable pieces of code, Called Blueprint (Usually called classes) which are used to create individual instances of objects.
CLASS's COMPONENTS:
1.Properties
Properties represent the attributes or data of an object. They define what an object 'Has'.
Properties typically include getters and setters methods to read and write values.
public class Car{
public string Make {get; set;}
public string Model {get; set;}
}
2.Fields
- Fields are variables declared within a class, They represent the internal data or state of an object. Fields usually have private access modifiers and are accessed through properties or methods.
public class Car {
private string _engineType; //Field
public string EngineType {
get{ return _engineType; }
set{ _engineType = value; }
}
}
3.Methods (Functions)
Methods define the actions or behaviors that an object can perform, They represent what an object can do.
Methods in a class can be instance methods (requiring an object instance to invoke) or static methods (which can be called without an object instance).
public class Car {
public void StartEngine() {
Console.WriteLine("Engine started");
}
public void StopEngine() {
Console.WriteLine("Engine stopped");
}
}
4.Constructors
- A constructor is a special method used to initialize new objects. It is called when an object is created from the class. It can set initial values for the object's properties or fields.
public class Car {
public string Make {get; set;}
public string Model {get; set;}
public Car(string make, string model) {
Make = make;
Model = model;
}
}
5.Static Members 👨👩👦👦
- Static Members (both fields and methods) belong to the class itself rather than any specific object instance. You can access static members using class name, not enough through an object instance.
public class Car {
public static int TotalCarsCreated {get; set;}
public Car() {
TotalCarsCreated++;
}
public static void DisplayTotalCars() {
Console.Writeline($"Total cars created : {TotalCarsCreated}");
}
}
Example usage :
Car myCar = new ();
Car.DisplayTotalCars(); //output : Total cars created : 1
6.Destructors
- A destructor is a special method used to clean up resources when an object is destroyed. This is less common in managed languages like C#, as garbage collection automatically handles memory management. However, destructors are still useful for releasing unmanaged resources.
- Access Modifiers 🔒
- Classes can define access modifiers that control the visibility and accessibility of their members (properties, field, methods, etc...).
-> Public : Accessible from anywhere
-> Private : Accessible only from the class itself
-> Protected : Accessible withing the class and derived classes
-> Internal : Accessible within the same assembly
private class Car{
//your code
}
public class Car{
//your code
}
protected class Car{
//your code
}
internal class Car{
//your code
}
CLASS as an ABSTRACT BLUEPRINT:
A Class acts as a Blueprint or Template for creating objects. It defines the properties(fields), Behaviours(methods), and the structure that its objects will have.
A Clss itself does not create an object directly; it is a definition of how the object will look and behave.
CONCRETE OBJECTS :
When you instantiate a class (using
new
keyword in C#), you create an object or an instance of that class.These object are referred to as concrete objects because they are actual instances created from the class blueprint
public class Car{
public string Make {get; set;}
public string Model {get; set;}
public void StartEngine()
{
Console.WriteLine("Engine Started");
}
}
//Creating concrete objects from the class:
Car car1 = new Car{Make = "BMW" , Model = "x6"};
Car car2 = new Car{Make = "Mercedes" , Model = "G63"};
car1.StartEngine(); //output : Engine Stated
car2.StartEngine(); //output : Engine Stated
The Car
class is the abstract blueprint (note that it doesn't represent actual car yet)
The Car1
and Car2
are concrete objects created from the Car
Class
THE FOUR PILLARS OF OOP :
1.Encapsulation
Definition Bundling data(fields) and methods that operate on that data into a single unit(class) and restricting access to some of the object's components.
Purpose To hide the internal state and behavior of an object and expose only what is necessary.
public class BankAccount{
private double balance;
public void Deposit(double amount) {
if(amount > 0 ) {
balance += amount;
}
public double Getbalance() {
return balance;
}
}
- Key Concept : Protects the object's integrity by preventing unauthorized access or modifications.
2.Inheritance
- *Definition * Allows one class (child or derived class) to inherit the properties and behaviors (methods) of another class (parent or base class)
** *Purpose * Promotes code reuse and establishes a hierarchical relationship between classes.
public class Animal {
public void Eat() {
Console.WriteLine("Eating...);
}
}
public class Dog : Animal {
public void Bark() {
Console.WriteLine("Barking");
}
}
- Key Concept : Enables the creatio of specialized classes based on existing ones.
3.Polymorphism
- Definition Allows objects of different classes to be treated as objects of a common base class. The same interface or method can behave differently based on the object invoking it.
There are two types :
- Compile-time Polymorphism (Method Overloading) : Multiple methods with the same name but different parameters.
public class Calculator {
//This takes 2 integers..
public int Add(int a , int b) {
return a + b;
}
//This takes 3 integers..
public int Add(int a , int b , int c) {
return a + b + c;
}
//This takes 4 integers..
public int Add(int a , int b , int c , int d) {
return a + b + c + d;
}
// Let's make instance of the Calculator class..
Calculator myCal = new();
Console.WriteLine(myCalc.Add(2,3)); //output : 5
Console.WriteLine(myCalc.Add(2,3,5)); //output : 10
Console.WriteLine(myCalc.Add(2,3,5,6)); //output : 16
}
So here it cames the Magic of the Polymorphism (Method Overloading), you just call the method can insert a values (must be 2 or 3 or 4 numbers as we declared before)...
2.Run-time Polymorphism (Method Overriding) : Methods in a derived class override the methods of a base class.
public class Animal {
public virtual void MakeSound() {
Console.WriteLine("Bla bla bla...");
}
}
public class Dog {
public override void MakeSound() {
Console.WriteLine("Bark");
}
}
Animal myDog = new();
myDog.MakeSound(); //output : Bark
Notice keyword Virtual
in the base class, It is added so other child classes can override it (make it possible to be overridden).
4.Abstraction
Definition Simplifying complex reality by modeling classes appropriate to the problem and providing a clear separation between what an object does and how it achieves it.
*Purpose * To hide complex implementation details and show only the relevant parts.
public abstract class Shape {
public abstract double CalculateArea();
}
public class Circle : Shape {
public double Radius { get; set; }
public override double CalculateArea() => Math.PI * Radius * Radius;
}
So These principles work together to provide a robust framework for designing flexible, modular, and maintainable software.
- Encapsulation protects an object's state.
- Inheritance promotes code reuse
- Polymorphism provides flexibility through dynamic behavior
- Abstraction hides complexity and provides clean interface.
To explore another approach to Object-Oriented Programming with a different explanation and style, check out my friend's insightful article
Introducing Object-Oriented-Programming
Rasheed K Mozaffar ・ Feb 9 '23
That's it for now! Keep coding and stay awesome. Catch you later!
Top comments (0)