Week 6 — What is the object-oriented paradigm?

Question of the Week #6
What is the object-oriented paradigm?
5 Replies
Eric McIntyre
Eric McIntyre2y ago
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:
public class SomeClass{//SomeClass is a class that is accessible from other files/classes (public)
private String someString="Hello World";//objects of SomeClass contain a reference to a String named someString. By default, it is set to the String "Hello World"
private int someInt;//objects of SomeClass contain an integer named someInt. By default, it is 0.
public void printInformation(){//The class defines a method called printInformation with no arguments and the method doesn't return anything (void). The method is public (accessible from other classes/files)
System.out.println(someString);//the method can access the attribute someString and print it
System.out.println(someInt);//the method can access the attribute someInt and print it
}
public void setValuesToSomethingElse(){//another public void method without parameters
someString="This is some other String.";//attributes can be changed in methods as well
someInt=1337;
}
}
public class SomeClass{//SomeClass is a class that is accessible from other files/classes (public)
private String someString="Hello World";//objects of SomeClass contain a reference to a String named someString. By default, it is set to the String "Hello World"
private int someInt;//objects of SomeClass contain an integer named someInt. By default, it is 0.
public void printInformation(){//The class defines a method called printInformation with no arguments and the method doesn't return anything (void). The method is public (accessible from other classes/files)
System.out.println(someString);//the method can access the attribute someString and print it
System.out.println(someInt);//the method can access the attribute someInt and print it
}
public void setValuesToSomethingElse(){//another public void method without parameters
someString="This is some other String.";//attributes can be changed in methods as well
someInt=1337;
}
}
After defining the class, it is possible to create objects (instances) of that class and use them like this:
SomeClass someObject;//create a variable of type SomeClass
someObject=new SomeClass();//create an object of that class using the new keyword (calling the constructor) and assign it to the variable
someObject.printInformation();//calls the method printInformation, prints Hello World and 0
someObject.setValuesToSomethingElse();//calls the other method which is capable of changing the object
someObject.printInformation();//calls the method printInformation, prints This is some other String. and 1337
SomeClass someObject;//create a variable of type SomeClass
someObject=new SomeClass();//create an object of that class using the new keyword (calling the constructor) and assign it to the variable
someObject.printInformation();//calls the method printInformation, prints Hello World and 0
someObject.setValuesToSomethingElse();//calls the other method which is capable of changing the object
someObject.printInformation();//calls the method printInformation, prints This is some other String. and 1337
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:
public class SomeClass{//SomeClass is a class that is accessible from other files/classes (public)
private String someString;
private int someInt;
public SomeClass(){//constructor requiring no arguments
someString="Hello";
someInt=1337;
}
public SomeClass(String s, int i){//constructor requiring a String and an int argument
someString=s;//sets the String attribute to the String argument
someInt=i;//sets the int attribute to the int argument
}
public void printInformation(){
System.out.println(someString);
System.out.println(someInt);
}
}
public class SomeClass{//SomeClass is a class that is accessible from other files/classes (public)
private String someString;
private int someInt;
public SomeClass(){//constructor requiring no arguments
someString="Hello";
someInt=1337;
}
public SomeClass(String s, int i){//constructor requiring a String and an int argument
someString=s;//sets the String attribute to the String argument
someInt=i;//sets the int attribute to the int argument
}
public void printInformation(){
System.out.println(someString);
System.out.println(someInt);
}
}
SomeClass someObject=new SomeClass();//create a new object of SomeClass by calling the constructor not requiring any arguments and assigning it to a newly created variable called someObject
SomeClass otherObject=new SomeClass("Hi",192);//create a new object of SomeClass by calling the constructor requiring two arguments (String and int) and assigning it to a newly created variable called otherObject
someObject.printInformation();//prints Hello and 1337
otherObject.printInformation();//prints Hi and 192
SomeClass someObject=new SomeClass();//create a new object of SomeClass by calling the constructor not requiring any arguments and assigning it to a newly created variable called someObject
SomeClass otherObject=new SomeClass("Hi",192);//create a new object of SomeClass by calling the constructor requiring two arguments (String and int) and assigning it to a newly created variable called otherObject
someObject.printInformation();//prints Hello and 1337
otherObject.printInformation();//prints Hi and 192
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.
public class SuperClass{
private String someString;
protected int someInt;//this attribute is protected i.e. it can be accessed from classes extending (inheriting) from SomeClass (but not generally from other classes)
public void printInformation(){
System.out.println(someString);
System.out.println(someInt);
}
public void setValuesToSomethingElse(){
someString="This is some other String.";
someInt=1337;
}
}
public class SubClass extends SuperClass{//SubClass extends/inherits SuperClass
@Override
public void setValuesToSomethingElse(){//this method "overrides" the method setValuesToSomethingElse of SuperClass i.e. provides a different implementation of that method
super.setValuesToSomethingElse();//this calls setValuesToSomethingElse of SuperClass (with the same object)
someInt=127;//set someInt to 127 after letting setValuesToSomethingElse do the work
//someString="abc";//this does not work because someString is private i.e. not accessible from other classes/files
}
}
public class SuperClass{
private String someString;
protected int someInt;//this attribute is protected i.e. it can be accessed from classes extending (inheriting) from SomeClass (but not generally from other classes)
public void printInformation(){
System.out.println(someString);
System.out.println(someInt);
}
public void setValuesToSomethingElse(){
someString="This is some other String.";
someInt=1337;
}
}
public class SubClass extends SuperClass{//SubClass extends/inherits SuperClass
@Override
public void setValuesToSomethingElse(){//this method "overrides" the method setValuesToSomethingElse of SuperClass i.e. provides a different implementation of that method
super.setValuesToSomethingElse();//this calls setValuesToSomethingElse of SuperClass (with the same object)
someInt=127;//set someInt to 127 after letting setValuesToSomethingElse do the work
//someString="abc";//this does not work because someString is private i.e. not accessible from other classes/files
}
}
SuperClass someObject=new SubClass();//actually create an object of type SubClass and assign it to the variable someObject of type SuperClass
someObject.setValuesToSomethingElse();//although this might seem like calling setValuesToSomethingElse from SuperClass, this actually calls the method from SubClass because the method is overridden for objects of SubClass
someObject.printInformation();//prints This is some other String. and 127
SuperClass someObject=new SubClass();//actually create an object of type SubClass and assign it to the variable someObject of type SuperClass
someObject.setValuesToSomethingElse();//although this might seem like calling setValuesToSomethingElse from SuperClass, this actually calls the method from SubClass because the method is overridden for objects of SubClass
someObject.printInformation();//prints This is some other String. and 127
Eric McIntyre
Eric McIntyre2y ago
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.
public class SomeClass{
private int someInt;
public void setSomeInt(int i){
someInt=i;
}
public int getSomeInt(){
return someInt;
}
}
public class SomeClass{
private int someInt;
public void setSomeInt(int i){
someInt=i;
}
public int getSomeInt(){
return someInt;
}
}
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.
public class SomeClass{
private int otherInt;
public void setSomeInt(int i){
otherInt=i;
}
public int getSomeInt(){
return otherInt;
}
}
public class SomeClass{
private int otherInt;
public void setSomeInt(int i){
otherInt=i;
}
public int getSomeInt(){
return otherInt;
}
}
A more concrete example of that would be a complex number class:
public class ComplexNumber{
private double real;
private double imaginary;
public double getReal(){
return real;
}
public double getImaginary(){
return imaginary;
}
}
public class ComplexNumber{
private double real;
private double imaginary;
public double getReal(){
return real;
}
public double getImaginary(){
return imaginary;
}
}
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:
public class ComplexNumber{
private double abs;
private double angle;
public double getReal(){
return abs*Math.cos(angle);
}
public double getImaginary(){
return abs*Math.sin(angle);
}
}
public class ComplexNumber{
private double abs;
private double angle;
public double getReal(){
return abs*Math.cos(angle);
}
public double getImaginary(){
return abs*Math.sin(angle);
}
}
⭐ Submission from dan1st#7327
Eric McIntyre
Eric McIntyre2y ago
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
Eric McIntyre
Eric McIntyre2y ago
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
Eric McIntyre
Eric McIntyre2y ago
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
Want results from more Discord servers?
Add your server