1. OOPS vs Procedural vs Functional - mStylias/JavaTopics GitHub Wiki
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 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 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.
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 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.
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 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.
- Reusability: Through classes and objects, and inheritance of common attributes and functions.
- Security: Hiding and protecting information through encapsulation.
- Maintenance: Easy to make changes without affecting existing objects much.
- Inheritance: Easy to import required functionality from libraries and customize them, thanks to inheritance.
- Beforehand planning of entities that should be modeled as classes.
- OOPS(Object-Oriented Programming System) programs are usually larger than those of other paradigms.
- 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.
- Therefore, might involve a slightly steep learning curve.
In this example we showcase the object oriented principles of polymorphism, inheritance, encapulation and abstraction
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:
- calculatePerimeter
- calculateArea
- 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
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.