
What is Object Oriented Programming?
Object Oriented Programming (or OOP) is one of the most popular programming paradigm in the world. It is a way of thinking, designing or replicate real world object in the code or program. When we talking about OOP, you must came across to terms like “class” and “object” because they form the backbone of how this programming paradigm operates. As well as 4 main pillar of OOP, Encapsulation, Abstraction, Inheritance and Polymorphism. We will be exploring what are all these term means and what are they all doing.
What is an Object?
An object is the fundamental building block of Object-Oriented Programming (OOP). It represents a real-world entity or concept and encapsulates two main components:
Attributes (data/state): An information or data that Object needs to know
- A “Car” object might have attributes like
color,make,model, andspeed. - A “Slime“ object need to know it’s
health,level,attack poweranddefence power.
Methods (behaviour): The actions or functionalities the object can perform.
- A “Car” object might have methods like
start_engine(),accelerate(), andstop(). - A “Slime‘ object need to be able to perform action like
Attack(),Walking(), orFlee().
Think of an object as a specific instance of something. When creating an object, always thinks about what is it need to know to be able to do what it needed to do.
What is a Class?
A class is a blueprint or template used to create objects. It defines the structure (attributes) and behaviour (methods) that the objects will have, but it doesn’t represent an actual entity until an object is created.
Key Differences Between Class and Object
Object
- A specific instance of a class.
- Holds actual data and performs actions.
- Concrete entity; memory is allocated.
Class
- A blueprint or template.
- Defines attributes and methods.
- Abstract concept; no memory allocated.
Example of creating class and object for Dog
We want to replicate real world object like Dog in our program, our first step is to think about what Dog should know and what can it do. Dog should be able to know information like it’s name, breed, weight and/or color. Dog should be able to do things like walking, breathing or sleeping.
Now that we’ve knew what attribute and method a Dog should have, we can create Dog class. This class will act as an blueprint to create Dog Object for our program, the Dog class specify that every Dog object should know their name, weight and it need to be able to perform action like walking, breathing and sleeping.
Dog Class in C#:
public class Dog
{
// Fields (Data or Information)
string name;
string breed;
string color;
double weight;
// Constructor
public Dog(string name, string breed, string color, double weight)
{
this.name = name;
this.breed = breed;
this.color = color;
this.weight = weight;
}
// Method (Behaviour, Functions or Action to be perform)
public void Walking()
{
Console.WriteLine("The Dog is walking.");
}
public void Breathing()
{
Console.WriteLine("The Dog is Breathing.");
}
public void Sleeping()
{
Console.WriteLine("The Dog is Sleeping.");
}
// Output Dog info to console
public void GetInfo()
{
Console.WriteLine($"Name: {name}\nBreed: {breed}\nColor: {color}\nWeight: {weight} kg\n");
}
}How to use Class to create a Object in C#
// Create new Dog object and assign to variable name 'Sam'
Dog Sam = new Dog("Sam", "Pomeranian", "White", 1.2);
// Now we can easily call methods inside Dog class using 'Sam' variable
// Or in a simple word, we can now ask Sam to perform actions specify in the Dog class
Sam.Breathing();
Sam.Walking();
Sam.Sleeping();
Sam.GetInfo();OUTPUT
The Dog is Breathing.
The Dog is walking.
The Dog is Sleeping.
// Dog Info
Name: Sam
Breed: Pomeranian
Color: White
Weight: 1.2 kgEncapsulation
Encapsulation bundles both attributes (data) and methods (functions) into a single unit, which we call a class. Think of encapsulation as putting both data and functions into a “ capsule.” This approach also restricts direct access to some of an object’s components, ensuring controlled interaction. By exposing only necessary elements through public methods (like getters and setters), encapsulation protects the integrity of the data and promotes modular, maintainable code.
ANALOGY
Imagine you meet someone new, and they want to get to know you. You decide to share only certain pieces of information with them, such as your name and gender, while keeping other details, like your address or bank account number, private.
- Your name and gender are public information. People can access them because you’ve decided to make them available.
- Your address and bank account number are private information. Only you or a trusted entity (like your bank or close family) can access these details.
To interact with this private information, someone must go through specific and controlled methods, like asking you politely for permission. For example:
This mirrors Encapsulation and the role of Access Modifiers in programming. Just like how you choose what information to share and how others can interact with it, a class in OOP uses private, public, and protected access modifiers to control how its attributes and methods are accessed or modified.
Abstraction
Abstraction involves hiding complex implementation details while exposing only the necessary functionality. It focuses on what an object does rather than how it does it, making the code easier to understand and use. In simpler terms, abstraction represents complex real-world objects in a simplified way. It also promotes code reusability and helps developers build adaptable and user-friendly systems by focusing on essential features while hiding unnecessary complexity.
EXAMPLE
public class Dog // Dog class
{
public void Bark() // Bark Method of Dog class
{
// Implementation of how the dog Bark
Console.WriteLine("Woof!");
}
}
// Create Dog Object
Dog Sam = new Dog("Sam", "Pomeranian", "White", 1.2);
// Now we can simply call Bark method without needing to know how it was implemented
Sam.Bark();Inheritance
Inheritance allows developers to create a new class based on an existing one. The existing class is called the parent class (or base class), while the new class is called the child class (or derived class). A child class inherits attributes and methods (or behaviours) from its parent class and can extend or override them based on specific needs. This feature promotes code reuse and makes it easier to maintain and extend functionality without rewriting existing code.
EXAMPLE
// Create Parent class
public class Vehicle
{
// Fields, Data, Attribute
string color;
// Constructor
public Vehicle(string color)
{
this.color = color;
}
// Method - Start a Vehicle
public void StartEngine()
{
Console.WriteLine("Vehicle is starting...");
}
}
// Child Classes that inherited Vehicle class
public class Car : Vehicle
{
// Constructor - this allow Car to store color value without needing to declare again.
public Car(string color) : base(color) { }
}
// Same here for Motorcycle
public class Motorcycle : Vehicle
{
public Motorcycle (string color) : base(color) { }
}
// Create an object from each class
Vehicle vehicle = new Vehicle("Red");
Car car = new Car("Green"); // Can accept color value
Motorcycle motorcycle = new Motorcycle ("Blue");
vehicle.StartEngine(); // vehicle can call its own method
// Car and Motocycle can call its parent method, StartEngine() in this case
car.StartEngine();
motorcycle.StartEngine();OUTPUT
Vehicle is starting...
Vehicle is starting...
Vehicle is starting...Notice that the program output same result 3 times. That’s because we call the same method from Vehicle class. This could be improve by applying Polymorphism concept.
Polymorphism
Polymorphism allows child classes that inherit from a parent class to redefine methods with the same name and parameters, enabling each subclass to react to the same action in its own unique way. This concept is also known as method overriding. For example, if different subclasses of a parent Vehicle class have their own implementations of a StartEngine() method, the same method call can produce different results depending on the specific object. Polymorphism increases flexibility and makes systems easier to scale and adapt.
Modified Version of Vehicle Example
public class Vehicle
{
// Fields, Data, Attribute
string color;
// Constructor
public Vehicle(string color)
{
this.color = color;
}
// Method - Start a Vehicle (allow child class to implement their own way to start an engine)
public virtual void StartEngine()
{
Console.WriteLine("Vehicle is starting...");
}
}
public class Car : Vehicle
{
public Car(string color) : base(color) { }
public override void StartEngine()
{
// Modified
Console.WriteLine("Car is starting...");
}
}
public class Motocycle : Vehicle
{
public Motocycle(string color) : base(color) { }
public override void StartEngine()
{
// Modified
Console.WriteLine("Motocycle is starting...");
}
}OUTPUT
Vehicle is starting...
Car is starting...
Motorcycle is starting...Now you can see that child class like Car and Motorcycle can perform their own way of Start Engine. In simple word, child class know thing like their parent class and can do same thing as their parent can but they can do it differently now.
Conclusion
Object-Oriented Programming (OOP) is a powerful paradigm that mirrors real-world entities in code, making it intuitive, modular, and adaptable for developers. By focusing on objects with their attributes (data) and methods (functions)-OOP provides a natural way to design and structure software systems.
The four pillars of OOP — Encapsulation, Abstraction, Inheritance, and Polymorphism work together to create robust and reusable code. Encapsulation ensures data integrity by controlling access, while abstraction simplifies complex systems by hiding unnecessary details. Inheritance promotes code reuse, allowing developers to build on existing functionality. Polymorphism adds flexibility by enabling objects to behave differently based on context, making systems more scalable and maintainable.