22 Oct 2023
Object-Oriented Programming (OOP) is built on fundamental principles that guide the design and structure of code. The four pillars of object-oriented programming are Encapsulation, Inheritance, Polymorphism, and Abstraction. Let's break them down.
-
Encapsulation: Encapsulation is the concept of wrapping data (attributes or properties) and methods functions (functions or behaviors) into a single unit called a class. The purpose of encapsulation is to prevent unauthorized alteration of data from outside. This data can only be accessed by getter functions or methods of the class. Encapsulation is a way of protecting data and providing controlled access, ensuring that data is only accessed according to predefined rules or agreements.
Examples:
- In a car, the data members such as engine, wheels, and fuel level are encapsulated together with the methods such as start(), stop(), and accelerate(). This prevents other objects from directly interfering with the car's operation. Instead, they must use the provided methods to control the car.
- In a bank account, the data members such as account number, balance, and name are encapsulated together with the methods such as deposit(), withdraw(), and getBalance(). This prevents other objects from directly accessing or modifying the account data. Instead, they must use the provided methods to interact with the account.
-
Inheritance: Inheritance allows you to create new classes (subclasses or child classes) based on existing classes (superclasses or parent classes). It promotes code reuse by inheriting attributes and methods from a parent class and allowing you to extend or modify them in the child class.
Examples:
-
Animal class with properties like name, age, and methods like eat(), sleep(), move().
- Dog class inherits from Animal class, gaining all its properties and methods. Dog class can also add its own specific behavior, like bark().
- Cat class inherits from Animal class, gaining all its properties and methods. Cat class can also add its own specific behavior, like meow().
-
Vehicle class with properties like make, model, year, and methods like start(), stop(), drive().
- Car class inherits from Vehicle class, gaining all its properties and methods. Car class can also add its own specific behavior, like honk().
- Motorcycle class inherits from Vehicle class, gaining all its properties and methods. Motorcycle class can also add its own specific behavior, like rev().
-
-
Polymorphism: Polymorphism is a key concept in object-oriented programming (OOP) that allows objects to take on different forms or behave in different ways depending on the context in which they are used. This is achieved through the use of inheritance, interfaces, and method overriding.
Examples:
- A car can be a sedan, truck, or SUV. All cars have a common set of functions, such as starting, stopping, and steering. However, each type of car has its own unique characteristics, such as fuel economy and cargo capacity. This is an example of runtime polymorphism, as the specific type of car being used is not known until runtime.
- A website can be viewed on a desktop computer, laptop, or mobile phone. The website will adapt its layout and functionality to the specific device being used. This is an example of runtime polymorphism, as the specific type of device being used is not known until runtime.
- A person can have multiple roles, such as a parent, child, employee, and friend. The person is a single entity that can take on many different forms.
- A computer program can be used to solve different types of problems. For example, a sorting program can be used to sort numbers, letters, or objects. The sorting program can be used to solve any problem that requires sorting. This is an example of runtime polymorphism.
-
Abstraction: Abstraction is the process of hiding the complexity of an object by only exposing its essential features and behaviors to the user. Abstraction allows the user to interact with the object without having to understand how it works internally.
Abstraction involves extracting essential details and features from an idea or concept while eliminating unnecessary or irrelevant elements . Abstraction helps in managing the complexity of large software systems.
Example:
- Using a bank ATM: You don't need to know how the ATM's internal mechanisms work in order to withdraw money. You simply insert your card, enter your PIN, and select the amount you want to withdraw. The ATM's abstraction hides the complexity of its underlying hardware and software.
- Using a washing machine: You don't need to know how the washing machine's internal mechanisms work in order to wash clothes. You simply select the desired cycle, add detergent, and press the start button. The washing machine's abstraction hides the complexity of its underlying plumbing and electrical systems.
- Using a computer: You don't need to know how the computer's hardware works in order to use software. You simply open applications, type documents, and browse the internet. The computer's abstraction hides the complexity of its underlying circuitry.