What is OOP (Object Oriented Programming)?
Object-oriented programming is a way programming that enables programmers to think like they are working with real-life entities or objects. In real life, people have the knowledge and can do various works. In OOP, objects have departments to store /state/data/knowledge and can do several works — methods.
How does object-oriented programming work?
When developing with OOP, the application and everything about how it works must be plotted out in a series of steps through mind-mapping and diagrams. The first question that every dev must answer is what the application or program will be able to do and how it will function.
They need to answer important questions on who will use the program, how much control they will have over the data in the system, how they will access it, and how data will be saved in the system. This can prove very time-consuming, but this step is required.
Another consideration that the dev will make is which classes will need to be involved in the program. Examples of a class include users, schools, colors, sizes, etc. These classes will make up a system, which the dev will organize into a class diagram. Doing this allows the program to make more visual sense. Once the planning is finished, the dev begins the coding process.
Encapsulation is a device of covering the data and code acting on the data unitedly as a single unit. The main purpose of encapsulation is you will have full control over data by using the code. In encapsulation, the variables of a class can be hidden from other classes and can only be entered through the processes of their current class.
Abstraction is a method of hiding the implementation details from the user, only the functionality will be given to the user. Abstraction is a very broad topic. If someone asks you on what abstraction is you can tell them the definition that we gave above.
There are 2 types of abstraction:
- Control Abstraction
- Data Abstraction
Programming languages offer control abstraction as one of the main goals of their use. Computer machines understand operations at a very low level like moving some bits from one location of the memory to another location and producing the sum of two sequences of bits. Programming languages allow this to be done at a greater level.
Without control abstraction, a programmer would need to define all the register/binary-level steps each time they simply wanted to increase or add a few numbers and assign the result to a variable. Such duplication of effort has two negative consequences:
- It demands the programmer to program for the particular hardware and instruction set.
- It forces the programmer to regularly repeat common tasks every time a similar process is needed.
Data abstraction is the way to create complex data types and only exposing important operations to cooperate with data types, whereas hiding all the implementation details from outside works. The benefit of this approach involves the capability of improving implementation over time.
The idea is that these changes are not supposed to have any influence on client code as they involve no difference in abstract behavior. Some things you need to know about abstractions:
- Abstractions are ideas, not specific events
- To abstract something is to walk away from its implementation and think about great ideas
- Abstractions can be used to organize code (but also many other things) effectively
- Object-oriented programming is completely dependent on the abstractions.
- Abstraction is the method of breaking a big problem into smaller parts so that each smaller problem can be worked on in (relative) isolation.
Inheritance can be described as the process where one class receives the properties of another.
The idea of inheritance implements is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well, etc.
Types of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Multi-Level Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
3 Advantages of inheritance:
- Inheritance promotes reusability. When a class inherits or derives another class, it can enter all the functionality of the inherited class.
- Reusability enhanced reliability. The first-class code will already be debugged and tested.
- As the existing code is reused, it leads to fewer maintenance costs development.
3 Disadvantages of inheritance are as follows:
- Inherited functions work slower than normal functions as there is indirection.
- Wrong-way of using inheritance may lead to wrong solutions.
- Inheritance increases the coupling between the base class and derived class. A change in the base class will affect all the child classes.
Inheritance is one of the most misused features of OOPs by beginner programmers.
In object-oriented programming, polymorphism is the characteristic of being able to choose another purpose or usage to something in diverse contexts — especially, to allow an entity like an object or function, to have more than one form.
There are 2 types of polymorphism implementations:
- Static Polymorphism
- Dynamic Polymorphism
Features of polymorphism mainly depend on the programming languages.
Signs of Bad Design
You also need to know about some signs of bad design so you can detect them on your code.
The software is hard to change, even for little items. Every modification requires cascading changes that take weeks to implement. The developers have no idea what will happen and what will have to be changed when they need to do Y or X. This leads to fear and hesitation to change in both the developers and the management. And that gradually makes the code very hard to maintain.
The software breaks in surprising ways for every change. This is a related problem to rigidity, but it’s different because there is not a sequence of modifications that continues to become longer by the hour. Everything seems to operate just fine, but when you think you are ready to ship the code, a test, or a customer, tells you that something else doesn’t work anymore.
The new thing works, but another thing is broken. Every fix is actually two new problems. This leads to existential dread in the developer that feels like it has lost control of the software.
Every module works, but only in the care situation, it has been placed in. You cannot reuse the code in another project, due to there being too many little things that you will have to replace. The program seems to work by dark magic. There is no design, only hacks.
Every time you change something you know what to do, but it’s always a terrible thing that makes you afraid that it will come back to attack you. And it will. Apart from disgrace, that you should really feel, it makes the code quite hard to reuse. Instead, each time you need to reuse the code, you will recreate a slightly different version, that almost does the same thing.