Week 6 — What is the object-oriented paradigm?
Question of the Week #6
What is the object-oriented paradigm?
5 Replies
Object-Oriented programming (OOP) is a paradigm/style of programming which is prominent in Java.
In OOP, an object refers to some data and methods (operations) that can access that data.
The data that is part of an object is structured in a certain way and classes define how that data is structured (attributes) and what methods are part of it.
Each object belongs to one class defining the types of data and the methods part of that object.
In Java, objects of a certain class are created by calling the constructor of that class using the
new
Keyword. If no constructors are specified explicitly, a default constructor is automatically generated which does not require any arguments and sets all attributes to their default value.
A class can look like the following:
After defining the class, it is possible to create objects (instances) of that class and use them like this:
Using a constructor, it is possible to define logic that is executed once an object of the class is created. A constructor can be defined similar to a method but it has no return type.
It is also possible to define different constructors with different arguments:
Another important notion of OOP is inheritance. Inheritance allows to define classes that "inherit" all attributes and methods from a different class.
The class that is inherited (extended) is called parent class or super class while the class inheriting/extending the super class is called subclass or child class.
In OOP and especially in Java, it is a common best-practice to make sure other classes cannot access attributes directly. The data an object stores is an implementation detail and should not be visible to code outside the class. This is done by making the attributes
private
and providing (public) methods for accessing these attributes if necessary.
Hiding attributes with getters and setters allows to change the implementation and data of an object without needing to modify other classes. For example, it would be possible to rename someInt
or change how data is stored internally without needing to rename/change the getter and setter and code using it.
A more concrete example of that would be a complex number class:
When the developer decides they would rather want to store it in polar form internally, they can modify it without changing any code using it:
⭐ Submission from dan1st#7327
Putting everything in their own objects.
That allows for stuff like representing real life objects with classes and their own objects, properties, etc, but it's also really verbose, which many people don't like
Submission from 0x150#3309
The object oriented paradigm (OOP) is the concept that programs are written and executed with a series of objects interacting. Java and C# are two languages which heavily adopt this paradigm.
Objects are instances of defined classes, which come with all the features we see in OOP like inheritance, polymorphism, generics, encapsulation, etc. We can also leverage many design patterns, like factory, singleton, facade, builder, and more.
Classes define their types, members, and internal workings; as well as having a lot of syntactical sugar to enable other features around this.
All classes inherit from a primitive type "object". This object type consists of few methods. hashCode and equals are used to define equality, tostring is for serialization, finalize is for garbage collection, and there area few others as well.
⭐ Submission from Fright XO#1337
The object-oriented paradigm is a way of programming in which abstractions are implemented using "objects" that hold data and interact with each other. Oftentimes, this is done using "classes," which act as a blueprint through which an object is created.
In practice, object-oriented programming is often very similar to modular programming in which different modules are independent of each other but are connected via "dependencies." In object-oriented programming, this dependence is usually not decoupled from the actual thing holding the data or functionality, which leads to very dependent and coupled code.
We will now look at features of object-oriented programming that are unique to the paradigm and thus make it up.
objects, classes, and methods
objects and classes are one of the fundamental building blocks of oop, and while they seem unique in their ability, they are not more than glorified structs with procedures. In that sense, they do not have any specific behavior that is different from structured and procedural programming.
implementation inheritance
Implementation inheritance refers to the practice of overloading methods on a subclass, which changes how the underlying object behaves. This is discouraged, though, in favor of composition, which leads to safer and more explicit code.
encapsulation
Encapsulation can be described by "public interface, private implementation," which refers to the practice of hiding implementation details. Important to notice is that the same principle is used in modular programming, and so we can see that this is also not unique oo.
Altogether, we can see that a lot of the things associated with object-oriented programming are either not inherent to the paradigm or are discouraged in favor of non-oo patterns.
Therefore, we can conclude that the object-oriented paradigm is simply borrowing from other paradigms.
Sources:
https://youtu.be/QyJZzq0v7Z4
https://www.hillelwayne.com/post/alan-kay/
Submission from jade#9418