Table of contents
Hi There - I'm starting this blog to share some tutorials and thoughts on the tech I'm working on. To start with, I'm explaining the OOPS concept as it's the most basic and required knowledge for almost all the coding skills. In the following posts I'll talk mostly on .Net, Dapr and related technologies. Most of you already know about OOPS, but I'm just trying to provide some details with practical example for easy understanding. Let's dive in.
In this blog post, we will explain each concept of OOPS using a laptop as an example.
What is OOPS? Object-oriented programming (OOP) is a paradigm that organizes data and behavior into reusable and modular units called objects. Objects have two main characteristics: attributes and methods. Attributes are the data or properties that describe the state of an object, such as color, size, model, etc. Methods are the functions or behaviors that define what an object can do, such as turn on, shut down, display, etc. OOP is based on four main concepts: abstraction, encapsulation, inheritance, and polymorphism.
Abstraction
Abstraction is the process of hiding the unnecessary or complex details of an object and exposing only the essential or relevant features. Abstraction helps to simplify the design and implementation of a system by focusing on what an object does rather than how it does it.
For example, a laptop is an abstract representation of a complex device that consists of many components, such as CPU, RAM, hard disk, keyboard, mouse, screen, etc. However, we do not need to know the details of how these components work or interact with each other to use a laptop. We only need to know the basic features of a laptop, such as turning it on, typing, browsing, etc.
Encapsulation
Encapsulation is the mechanism of bundling the attributes and methods of an object together and restricting the access to them from outside. Encapsulation helps to protect the data and behavior of an object from being modified or misused by other objects or code. Encapsulation also enables information hiding, which is a form of abstraction that conceals the internal implementation of an object.
For example, a laptop has many attributes, such as battery level, volume, brightness, etc. and methods, such as charge, mute, dim, etc. However, these attributes and methods are not directly accessible or visible to the user or other devices. They are encapsulated within the laptop object and can only be accessed or modified through predefined interfaces, such as buttons, sliders, menus, etc.
Inheritance
Inheritance is the process of creating new classes of objects based on existing classes of objects. Inheritance allows the new classes to inherit the attributes and methods of the existing classes and add new ones or override them as needed. Inheritance helps to avoid code duplication and promote code reuse by creating a hierarchy of classes that share common features and behavior.
For example, a laptop is a subclass of a computer, which is a subclass of an electronic device. A laptop inherits the attributes and methods of a computer, such as processor, memory, operating system, run, etc. and an electronic device, such as power, voltage, current, etc. A laptop also adds new attributes and methods, such as battery, screen, keyboard, charge, display, etc. or overrides existing ones, such as run, which is different for a laptop and a desktop computer.
Polymorphism
Polymorphism is the ability of an object to take different forms or behaviors depending on the context or situation. Polymorphism allows the same code or interface to work with different types of objects without knowing their exact details or implementation. Polymorphism helps to achieve flexibility and adaptability in a system by allowing objects to interact with each other in a generic way.
For example, a laptop can be used as a different type of device depending on the user's needs or preferences. A laptop can be used as a tablet by detaching the keyboard and using the touch screen, as a desktop by connecting a monitor and a mouse, as a gaming console by attaching a controller and a headset, etc. The same laptop object can perform different functions or behaviors depending on the mode or configuration it is in.
Hope this helped you get to know about OOPS.