• Miscellaneous
  • 6 MINUTES READ

The Fantastic Four of OOP

  • POSTED ON
  • December 9, 2020
  • POSTED BY
  • Muhammad Ahmad
  • POSTED ON December 9, 2020
  • POSTED BY Muhammad Ahmad

In today’s technologically driven society, computers are changing the way society behaves. Each one of you is keen on getting some knowledge of computer programming due to the high demand. But, why is Object-Oriented Programming(OOP) valuable? Many of you have embraced it because it makes the development and maintenance of a software program seem effortless.

In today’s technologically driven society, computers are changing the way society behaves. Each one of you is keen on getting some knowledge of computer programming due to the high demand.

But, why is Object-Oriented Programming(OOP) valuable?

Many of you have embraced it because it makes the development and maintenance of a software program seem effortless. Added benefits include modularity, extensibility, and reusability.

title countalt

Let’s Learn Object-Oriented Programming(OOP) Basics

What is object-oriented programming?

It is a computer programming model that revolves around the concept of using data, or objects, other than functions and logic. The object here is termed as a ‘data field’ that has typical attributes and behaves in a specific way.

You can use those objects in your code and perform different actions in relation to them.

The most frequently used OOP languages are Java, C++, C#, Python, R, PHP, Visual Basic.NET, JavaScript, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Kotlin, etc.

But the proper programming concepts are commonly used in Java, C++, and PHP because these languages simplify the structure and design of the software programs.

On top of this, object-oriented programming(OOP) concepts are mostly used to cater to large and complex software.

title countalt

To get started with Object-Oriented Programming basics; you should have an idea of what SOLID Principles are. OOP has five SOLID Principles that are impossible to avoid for developing software. Those are:

  1. SRP – Single Responsibility Principle
  2. OCP – Open/Closed Principle
  3. LSP – Liskov Substitution Principle
  4. ISP – Interface Segregation Principle
  5. DIP – Dependency Inversion Principle

These principles are used as a coding standard when applied in a code, makes it seem more logical and easier to read. Sometimes while working on a rough software design, the code becomes too complex and less flexible. Even little changes can give birth to difficult bugs; SOLID principles can be applied here to avoid such problems.

In a nutshell, SOLID principles can improve the quality of your code, and make the interface easier to understand.

We are going to try to explain SOLID Principles in the easiest way possible, so let’s go through each principle in sequence.

1- SRP – Single Responsibility Principle

A class should have one, and only one reason to change.

What is the most essential quality of a software code?

No matter how great your code looks, its quality has to be 100% maintained. Your code needs to adapt to change otherwise, it will go obsolete.

So in software and design engineering, a code has to stay updated.

SRP principle says that each class should have its sole function and must not serve more than one purpose. If you assign a class more than one condition, it will be hard for you to maintain its quality and there will be a lot of confusion left.

Today is the day you stop writing these gigantic codes without applying the SRP principle!

2- OCP – Open/Closed Principle

The rule states that:

Entities should be open for extension but closed for modification.

Robert C. Martin is the one who defined this principle and he considered it the most important among the other four.

Here is what the principal teach.

For example, you wrote a code but you now want to add some new functionalities into it, but you don’t want the already written code to be disturbed.

The idea of this principle allows you to achieve the above and make it possible to then modify the functionality of our code without changing the original code.

A Real-time Example:

Do you know that there are 16 different types of coffee machines these days?

There is a drip coffee maker, a very easy solution to make fresh coffee. Then there are brewer makers, capsule coffee makers, and many more.

Though they are dissimilar in shape, size, and function-wise, they serve the same purpose: to make delicious coffee for you that you can enjoy at any time of the day.

If we apply the OCP principle here, we know that the specs of a drip coffee maker are slightly different from the capsule maker.

If an electronic designer has to design a capsule maker by using the same drip coffee maker formula, he will not re-engineer the product but will add small additional features to the old design to reform it into a new one.

3- LSP – Liskov Substitution Principle

The rule states that:

Subtypes must be substitutable for their base types.

The principle says that ‘any class that is the child of a parent class must be useable in place of its parent without any change in its behavior.’

The four conditions that every coder needs to abide by are:

  1. Method signatures must match
  2. The preconditions for any method can’t be greater than that of its parent
  3. Postconditions must be at least equal to that of its parent
  4. Exception types must match

4- ISP – Interface Segregation Principle

The rule states that:

Clients should not be forced to depend on methods that they do not use.

The principle says that you should break interfaces into many smaller ones, so they better fulfill your exact needs.

A Real-time Example:

Xerox is an American corporation that sells print and digital document products and services.

It once designed a printer system that could conduct some basic tasks such as faxing or stapling for them.

Initially, the built software design was basic. But with time, it needed certain modifications.

The designers had to put in a lot of effort to make a single new modification in the software, re-deploy it, and that made the development an overwhelming job.

The design problem was that a single Job class was used for almost all of the tasks.

Martin was looking after the corporation, and he suggested a solution that is now known as the Interface Segregation Principle. The solution said that ‘other than having one large job class, each task must have a separate job class.’

5- DIP – Dependency Inversion Principle

The rule states that:

High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.

By using this principle, a module can be replaced with another module by changing the dependency module. The best part is, the low-level module won’t bring an impact on the high-level module.

title countalt

The Four Basics of Object-Oriented Programming?

There are four basic concepts of OOP. Let us give you a general idea of how they work and how they can be applied to a computer program. They are as follows:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

1- Encapsulation

In Object-Oriented Programming basics, encapsulation is explained as “packing data and information under a single unit.”

The whole idea behind encapsulation is to hide the implementation details from the users. If a data member is private, it means it can only be accessed within the same class. No outside class can access its private data members.

Let’s take the example of a calculator.

We just know that we have to press ‘3’, ‘+’, ‘2’, and then ‘=’ to get the result. We don’t need to know how it works.

2- Abstraction

Data abstraction means “exhibiting only necessary information to the outside world while hiding the background details.” It only provides a generalized view of classes.

Abstraction and Encapsulation are often mixed up, but actually, they are different concepts. Following are the main differences:

Abstraction shows only useful data by providing the most necessary details whereas Encapsulation wraps code and data for necessary information.

Abstraction is focused mainly on what should be done while Encapsulation is focused on how it should be done.

Abstraction solves problems at the design level while Encapsulation solves problems at the implementation level.

We can implement abstraction using abstract class and interfaces, whereas encapsulation can be implemented using by access modifier i.e. private, protected, and public.

If we take again the real-world example of a calculator, Encapsulation is the internal circuits, battery, etc., that combine to make it a calculator.

Abstraction is the different buttons like on-off, clear, and other buttons provided to operate it.

3- Inheritance

It is a property in which ‘one class acquires the property of another class.’

In inheritance, we have:
a) Sub Class: The class that inherits properties from another class.
b) Super Class: The class whose properties are inherited by the subclass.

Hence a sub-class (child Class) will inherit all public and protected properties and methods of its parent (Super Class), in addition, it may have its own properties and methods.

4- Polymorphism

Polymorphism is the ability of an object to take on many forms. More specifically, it is the ability to redefine methods for derived classes.

For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles, and triangles. No matter what shape an object is, applying the area method to it will return the correct results.

In a Nutshell

How OOP is applied in JavaScript?

Object-Oriented Programming has taken root in JavaScript since its establishment. It’s used to such an extent that JavaScript’s native functions and methods are written in the Object-Oriented style.

Take note of this that JavaScript influences OOP developers to its ecosystem. It offers easy ways to making prototypes and organizing related data.

As JavaScript is widely used for web development, do you want the best JavaScript web development services?

You can get on a call with us for a 360-degree review to discuss the design and development plan for a successful product launch.

ABOUT THE AUTHOR

Muhammad Ahmad

Currently serving as the SEO Manager at vteams, Ahmed is a highly skilled individual with several years of experience of Digital Marketing.

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.