Object-oriented programming relies greatly on the concept of classes and objects, which in turn contain functions and data. As explained, a class is an established blueprint (or prototype) from which objects are built. Thus, classes represent a set of methods (or properties) that are common to a certain object's type. In turn, an object is the basic unit of OOP and represents real-life entities. An object must have:
â
â
For instance, let's imagine we have "Athlete 1" as an object, and within that object, we have all the data about the object through the properties. Thus, the state could be sport, height, weight, trophies, country, etc. These properties store data, and an object's data can be manipulated through functions attributed to an object. In this case, this object's methods could be attack, defense, jump, run, sprint, etc. Further, the developer can create properties by declaring variables in the object's code module.
â
In sum, in OOP languages, the data is stored in the properties, and the logic behind lies in the functions and respective methods. Regarding object-oriented programming, methods are functions that belong to a class or object; methods are "owned" by a specific class or even object. In comparison, functions are "free", meaning they can be on any other scope of the code, not belonging to classes or objects. Therefore, a method is always a function, but a function is not always a method. When objects contain properties and methods that work closely together, those objects belong to the same class.
â
In an OOP language, code is written to define the classes and, consequently, the respective objects. Pure object-oriented languages follow the four core principles: encapsulation, abstraction, inheritance, and polymorphism.
â
â
â
Let's start by focusing on encapsulation. Encapsulation is highly important in OOP since it consists of the ability to encapsulate variables within a class from outside access. Â Properties and methods can be private or public. OOP languages allow developers to establish multiple degrees of visibility. On the one hand, private features can only be visible for the class itself. On the other hand, public features can be visible to everyone.
â
Inheritance is also extremely vital since it provides a mechanism to organised and structure the software. It allows classes to inherit states and behaviours from their superclasses, which also means that this principle supports reusability.
â
Mutable vs. ImmutableObject-oriented programming can support mutable data. Contrarily, functional programming uses immutable data instead. In both programming paradigms, an immutable object refers to an object whose state cannot be modified once created. A mutable object consists of exactly the opposite; an object's state can be modified even after being created.
â
In pure functional programming languages (e.g., Haskell), it is impossible to create mutable objects. Thus, objects are typically immutable. In OOP languages, the answer is not that straightforward since it depends more on the specifications of each OOP language. String and concrete objects can be expressed as immutable objects in order to improve runtime efficiency as well as readability. Plus, immutable objects can be very helpful when handling multi-threaded applications because it avoids the risk of the data being changed by other threads.
â
Mutable objects also have their advantages. They allow developers to make changes directly in the object without allocating it, saving time and speeding up the project. However, it is up to the developer and the development team to decide whether it actually pays off according to the project's objectives. For instance, Â mutation can also open more doors for bugs, but sometimes its speed is very suitable and even necessary.
â
Therefore, OOP Â can support mutability, but its languages may also allow for immutability. Java, C++, C#, Python, Ruby, and Perl can be considered object-oriented programming languages, but they do not support mutability or immutability exclusively. For example, in Java, the strings are immutable objects. Nonetheless, Java also has mutable versions of strings. Similarly, in C++, developers can declare new class instances as immutable or as mutable. Another good example is Python, which has built-in types that are immutable (e.g., numbers, booleans, frozensets, strings, and tuples); however, the custom classes are usually mutable.
â
It is also important to keep in mind that many of the mentioned languages are not 100% functional programming or object-oriented. For instance, Python is one of the most popular languages, and it truly is a multi-paradigm language. Thus, it can entail a more functional or OOP approach according to the developers' preference.
â
Imperative vs DeclarativeDeclarative programming is a programming paradigm that declares what the program has to accomplish. It does not declare how the program should accomplish a certain computation throughout the control flow; it just declares what it wants without explaining how to get it. In contrast, imperative programming relies on a sequence of statements to modify a program's state, providing a detailed description for each step on how to accomplish a certain goal.
â
The majority of OOP languages were designed to follow imperative programming primarily. In comparison, functional programming tends to follow a more declarative programming approach since its logic does not explicitly describe the flow control to achieve a certain output. Instead, it expresses a computation as a pure function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4