An experienced programmer knows when and why to use object oriented programming because they have a strong understanding of how to use it along with the benefits that come with using it. However, a number of beginners tend to question why they should use object oriented programming.
Aside from not having mastered the use of it yet and not having a thorough understanding of it like how a seasoned software engineer would, I think part of the reason for that unwillingness to use OOP comes from a lack of experience of working on big complex projects where object oriented programming is arguably mandatory to use. Even for accomplishing tasks that might not necessarily be considered very complex, object oriented programming can still some times be a more optimal technique to use than to not use.
Object oriented programming can save a lot of time and digital ink. It can accomplish particular tasks with less lines of code and is often used to create frameworks and/or engines that can be recycled and reused in multiple different programs.
If I wanted to store information about multiple characters along with some of their attributes then display those attributes for each character without using object oriented programming, this Java example does a good job exhibiting the difference in lines of code and time being consumed...
public class Main {
public static void main(String[] args) {
String character1Name = "Goyolo";
int character1HP = 100;
int character1Strength = 90;
int character1Defense = 80;
float character1Speed = 90;
String character2Name = "Raikami";
int character2HP = 100;
int character2Strength = 75;
int character2Defense = 75;
float character2Speed = 100;
String character3Name = "Magus";
int character3HP = 100;
int character3Strength = 100;
int character3Defense = 100;
float character3Speed = 50;
String character4Name = "Meijin";
int character4HP = 90;
int character4Strength = 90;
int character4Defense = 90;
float character4Speed = 90;
String character5Name = "Zaigabon";
int character5HP = 100;
int character5Strength = 100;
int character5Defense = 100;
float character5Speed = 100;
System.out.println(character1Name + "'s " + "HP is " + character1HP + ".");
System.out.println(character1Name + "'s " + "strength is " + character1Strength + ".");
System.out.println(character1Name + "'s " + "defense is " + character1Defense + ".");
System.out.println(character1Name + "'s " + "speed is " + character1Speed + ".");
System.out.println(character2Name + "'s " + "HP is " + character2HP + ".");
System.out.println(character2Name + "'s " + "strength is " + character2Strength + ".");
System.out.println(character2Name + "'s " + "defense is " + character2Defense + ".");
System.out.println(character2Name + "'s " + "speed is " + character2Speed + ".");
System.out.println(character3Name + "'s " + "HP is " + character3HP + ".");
System.out.println(character3Name + "'s " + "strength is " + character3Strength + ".");
System.out.println(character3Name + "'s " + "defense is " + character3Defense + ".");
System.out.println(character3Name + "'s " + "speed is " + character3Speed + ".");
System.out.println(character4Name + "'s " + "HP is " + character4HP + ".");
System.out.println(character4Name + "'s " + "strength is " + character4Strength + ".");
System.out.println(character4Name + "'s " + "defense is " + character4Defense + ".");
System.out.println(character4Name + "'s " + "speed is " + character4Speed + ".");
System.out.println(character5Name + "'s " + "HP is " + character5HP + ".");
System.out.println(character5Name + "'s " + "strength is " + character5Strength + ".");
System.out.println(character5Name + "'s " + "defense is " + character5Defense + ".");
System.out.println(character5Name + "'s " + "speed is " + character5Speed + ".");
}
}
Now if I were to use object oriented programming to accomplish the same task, I can just create a Character class with instance fields, a constructor to store data into the instance fields upon object creation and a method for displaying the data associated with each Character object.
class Character{
private String name;
private int HP;
private int strength;
private int defense;
private float speed;
public Character(String name, int HP, int strength, int defense, float speed) {
this.name = name;
this.HP = HP;
this.strength = strength;
this.defense = defense;
this.speed = speed;
}
public void displayAttributes(){
System.out.println(name + "'s " + "HP is " + HP + ".");
System.out.println(name + "'s " + "strength is " + strength + ".");
System.out.println(name + "'s " + "defense is " + defense + ".");
System.out.println(name + "'s " + "speed is " + speed + ".");
}
}
public class Main {
public static void main(String[] args) {
Character goyolo = new Character("Goyolo", 100, 90, 80, 90);
Character raikami = new Character("Raikami", 100, 75, 75, 100);
Character magus = new Character("Magus", 100, 100, 100, 50);
Character meijin = new Character("Meijin", 90, 90, 90, 90);
Character zaigabon = new Character("Zaigabon", 100, 100, 100, 100);
goyolo.displayAttributes();
raikami.displayAttributes();
magus.displayAttributes();
meijin.displayAttributes();
zaigabon.displayAttributes();
}
}
With the Character class created, it only takes 1 line of code to create 1 character object and store data into it via the constructor. And it only takes another line of code to display the data associated with that 1 character object by invoking the displayAttributes() method. Without OOP it took 5 lines of code to create variables and 4 lines to display the values of those variables for 1 character. That ratio is 2 lines of code with OOP vs 9 lines of code without OOP. The above example uses 5 characters so the ratio ends up being 10(with OOP) to 45(without OOP). The Character class had to be created but that's a small price to pay. If you imagine a scenario where you had to create a larger number of objects like 50 characters rather than just 5, it becomes more blatant and clear how much time and digital ink is being saved by using object oriented programming and therefore why object oriented programming is the better way to go in that situation. Even though it wasn't for the sake of the example, the Character class can also be declared public and put in another file(Character.java) so that it can be easily recyclable and reusable for other programs.