Object Oriented Programming

Gani Demirel
4 min readMar 4, 2023

Hello everyone, today I’d like to talk about object-oriented programming. This programming paradigm is crucial for software performance and maintenance, and it is widely used in the programming world as a result.

The object-oriented programming approach is based on the idea of separating different components within a program and having them communicate with each other. This allows for more modular, understandable, and manageable code to be written. Additionally, this approach can be used in different programming languages and is essential for software design and maintenance.

With the object-oriented programming approach, different components in a program can be independent of each other. This means that changing or rearranging any one component does not affect the others. This is significant for software maintenance because changing one component is easier and faster than rewriting the entire program.

Furthermore, the object-oriented programming approach is essential for software performance. For example, if communication between program components occurs through an interface rather than directly, the program’s performance improves. This results in faster and more efficient program operation.

Here are the Object-Oriented Programming concepts and their explanations with C# examples:

Classes: Classes are the fundamental building blocks of Object-Oriented Programming, which group related variables and functions within a program. Classes define where objects are created and determine the properties and behaviors of objects.

public class Person 
{
public string Name { get; set; }
public int Age { get; set; }
}

In this example, we have defined a class called Person with two properties Name and Age. This class can be used to create objects representing individual persons.

Objects: Objects are instances of classes that are used for a specific purpose within a program. An object can have the variables defined in its class and can call the functions defined in its class.

Person person1 = new Person() { Name = "John", Age = 30 };

Here we have created an object person1 of type Person using the new keyword. We have also initialized the Name and Age properties of this object.

Inheritance: Inheritance allows a class to inherit the properties or functions from another class. This makes the programming process more efficient and prevents code duplication.

public class Employee : Person 
{
public string JobTitle { get; set; }
}

In this example, we have defined a class called Employee which inherits from the Person class. The Employee class has an additional property called JobTitle.

Encapsulation: Encapsulation is used to protect the properties and functions of a class or object. Encapsulation hides the properties and functions within the class or object and makes them accessible only through certain functions.

public class BankAccount 
{
private decimal balance;

public void Deposit(decimal amount)
{
balance += amount;
}

public void Withdraw(decimal amount)
{
if (amount > balance)
{
throw new Exception("Insufficient funds");
}
balance -= amount;
}
}

In this example, we have defined a class called BankAccount with a private field balance. We have also defined two public methods Deposit() and Withdraw() to modify the balance. The balance field is encapsulated and can only be accessed through these methods.

Polymorphism: Polymorphism enables working with different data types using functions that have the same name but different functionalities. This makes the programming process more efficient and prevents code duplication.

public class Animal 
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}

public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog barks");
}
}

public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat meows");
}
}

In this example, we have defined a base class Animal with a virtual method MakeSound(). We also have two derived classes Dog and Cat which override the MakeSound() method to make different sounds.

Abstraction: Abstraction is the process of hiding unnecessary details and exposing only important features of a program. Abstraction is a crucial concept in Object-Oriented Programming. Abstraction enables multiple classes to share the same interface and provide their own implementation details. Abstraction makes programs more modular, reduces code duplication, and makes the programming process more efficient.

public interface IShape 
{
double GetArea();
}

public class Circle : IShape
{
private double radius;

public Circle(double radius)
{
this.radius = radius;
}

public double GetArea()
{
return Math.PI * radius * radius;
}
}

In this example, we have defined an interface IShape with a method GetArea(). We also have a concrete class Circle which implements the IShape interface and calculates the area of a circle using the GetArea() method. The interface serves as an abstraction of the concept of a shape and allows for different shapes to be represented by different classes.

These six concepts form the fundamental building blocks of Object-Oriented Programming, making the programming process more modular, understandable, and manageable. Each concept plays an important role in the programming process and improves program performance and maintenance.

In this blog post, we have explained what Object-Oriented Programming is and its 6 important concepts. In conclusion, the object-oriented programming approach is vital for software performance and maintenance. This approach is widely used in the programming world and can be implemented in any programming language. If you prioritize software performance and maintenance, I highly recommend learning object-oriented programming.

Thank you for reading.

--

--

Gani Demirel

Experienced full stack developer with a focus on creating beautiful and functional web pages. Proficient in .Net Core, MVC, and React.