1. OOPS vs Procedural vs Functional - mStylias/JavaTopics GitHub Wiki

Definitions

Object Oriented Programming

Object oriented programming can be defined as a programming model which is based upon the concept of objects. Objects contain data in the form of attributes and code in the form of methods. In object oriented programming, computer programs are designed using the concept of objects that interact with real world. Object oriented programming languages are various but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.

Java is an object oriented language. However, note that java can use both Object Oriented and functional programming simultaneously. The languages that have this ability are called multi-paradigm

Procedural Programming

Procedural Programming can be defined as a programming model which is derived from structured programming, based upon the concept of calling procedure. Procedures, also known as routines, subroutines or functions, simply consist of a series of computational steps to be carried out. During a program’s execution, any given procedure might be called at any point, including by other procedures or itself.

Functional programming

Functional programming is an approach to problem solving that treats every computation as a mathematical function. The outputs of a function rely only on the values that are provided as input to the function and don't depend on a particular series of steps that precede the function. Functional programming relies heavily on recursion. A recursive function can repeat itself until a particular condition is reached. This is similar to the use of iteration in procedural programming, but now applied to a single function as opposed to a series of steps. Examples of functional programming languages include Erlang, Haskell, Lisp and Scala.

Principles of OOP

  • Polymorphism

In common words, polymorphism refers to the ability of multiple objects sharing the same name, but having different structures or serving different functionalities in different contexts. Polymorphism can be easily observed in function overloading and function overriding.

  • Inheritance

Inheritance is one of the important concepts in OOPs that allows (child) classes to establish a sense of hierarchy by inheriting the attributes and methods of another (parent) class. This reduces redundancy as classes can share common logic, structure, and attributes while enforcing a clear hierarchy.

  • Encapsulation

This refers to the wrapping up of the contents of an entity into one unit. In OOPs terms, this refers to the tying up, and wrapping of class or object attributes (state) with their methods (behavior). Thanks to encapsulation, objects can have their own private state which can not be accessed by other objects, unless their methods or attributes are declared public. This aspect of OOP allows for more secure software implementations.

  • Abstraction

Abstraction in OOPs terms refers to the ability of classes to expose certain data attributes while keeping others private. This is usually done to hide the implementation details from the outside world, either to make things less complex, or more secure. This is done with the help of various access specifiers that specify the visibility of each class attribute.

Pros-Cons of OOP

  • Advantages

  1. Reusability: Through classes and objects, and inheritance of common attributes and functions.
  2. Security: Hiding and protecting information through encapsulation.
  3. Maintenance: Easy to make changes without affecting existing objects much.
  4. Inheritance: Easy to import required functionality from libraries and customize them, thanks to inheritance.
  • Disadvantages

  1. Beforehand planning of entities that should be modeled as classes.
  2. OOPS(Object-Oriented Programming System) programs are usually larger than those of other paradigms.
  3. Even though OOP systems resemble the real world in their logical entities, it might take some time to get the hang of thinking about the flow of your code in terms of classes and objects.
  4. Therefore, might involve a slightly steep learning curve.

Example implementation details

In this example we showcase the object oriented principles of polymorphism, inheritance, encapulation and abstraction

Structure

The example consists of the following structure:
Shape : The base class for all shapes
Triangle: The triangle shape -> extends Shape
Rectangle: The rectangle shape -> extends Shape

Also there is an interface called IBasicShapeOperations that contains the methods:

  1. calculatePerimeter
  2. calculateArea

Showcased Principles

  • The encapulation principle is demonstrated by every class, since they all have both private and public fields along with getters and setters in order to achieve code security
  • The Abstraction principle is demonstrated by the IBasicShapeOperations interface, which hides the implementation details and complexities.
  • The inheritance principle is demonstrated by both Triangle and Rectangle classes, since they extend the Shape class and have an IS-A relationship with it.
  • The polymorphism principle is demonstrated in the Main class, where an ArrayList of type Shape is created and all shapes (inluding Triangles and Rectangles) are added to it. After that their common methods are called, but perform differenty in each one

Sources and more details

Pros-Cons of each programming style: https://scoutapm.com/blog/functional-vs-procedural-vs-oop
Object Oriented vs Procedural: https://www.geeksforgeeks.org/differences-between-procedural-and-object-oriented-programming/
Procedural vs Functional: https://study.com/academy/lesson/functional-programming-and-logic-programming.html#:~:text=Procedural%20programming%20uses%20a%20very,computation%20as%20a%20mathematical%20function.

⚠️ **GitHub.com Fallback** ⚠️