Object Oriented Programming

Object Oriented Programming (abrreviated as OOP) is a coding paradigm (style in which code is organized) which organizes code into classes and objects.

Java is one of the most popular and well known OOP based languages and is the language I'll use for my demonstration of my OOP learning.

Factory

I like to think of OOP as factories (commonly it's likened to blueprints but I like my analogy better >:)

Imagine you are an entrepreneur, fresh out of college (or maybe you dropped out) and you're ready to show the world your brilliant ideas and products.

Creating a Class (Factory)

Let's say you buy a factory. Here's your new factory:

//Creates a class
public class Factory{ 
    
}

Variables and Constructor (Factory Data)

Pretty empty huh. Let's try making a frame work to plan out your factory and statistics we'll need to keep track of as we obtain equipment and come up with what it's going to produce.

To do this, we'll initialize variables, create a constructor, and make an empty function meant to be the instructions for what the factory needs to produce.

import java.util.ArrayList; //Import ArrayList class

public class Factory{
  //Initialize Variables: Variables for our generic Factory
  String owner;
  static double factoryProfit; //static variable -> belongs to class (our factories); the profit of our factories

  //Constructer (more details later)
  public Factory(String owner){ 
    //When this constructer is called, we will have to fill out some info which will be assigned to our new factory's (object's) data
    this.owner = owner;
  }

  public void production(int days){ //Function (return type: void); produces our product
    
  }

}

Classes Part 2 (Products & Product Data)

Now, we can't just start your factory if it doesn't make anything. Let's say you decide on starting out with toy production.

Let's create a toy class (our blueprint) to build toys!

//Create Toy class
public class Toy{
  ArrayList<String> materials = new ArrayList();
  double cost;
  double price;
  //Toy Constructor
  public Toy(ArrayList<String> materials, double cost, double price){
    this.materials = materials;
    this.cost = cost;
    this.price = price;
  }

  public void function(){ //Function of the toy
    
  }
}

Inheritance (Teddy Bears)

Good work! We're almost there. As a suggestion, Teddy Bears are quite popular! Let's consider making that your first product.

Below, let's create a child class that inherits its properties from our toy class! We call super to access the superclass's constructor.

//TeddyBear is a child class of the parent class Toy (inherits all of the variables and methods of Toy)
public class TeddyBear extends Toy { 
  //Unique variable for TeddyBear
  String color;
  //TeddyBear constructor
  public TeddyBear(ArrayList<String> materials, double cost, double price, String color){
    super(materials, cost, price); //super() references all the parent classes' constructors
    this.color = color;
  }

  public void function(){
    System.out.println("Woah a teddy bear! It's " + color + " and gives great hugs!");
  }

  public static TeddyBear createBrownTeddyBear(){ //Static method that instantiates (creates) a TeddyBear object w/ TeddyBear return type
    //Specifies the materials, price, and cost of a a 'Brown' TeddyBear
    ArrayList<String> materials = new ArrayList<String>();
    materials.add("Cotton");
    materials.add("Mohair");
    materials.add("Ribbon");
    materials.add("Plastic Eyes");
    double cost = 5.00;
    double price = 10.00;

    //Instantiates an object of itself (almost like a factory) and returns the object
    TeddyBear teddyBear = new TeddyBear(materials, cost, price, "Brown");
    return teddyBear;
  }
}

Instantiation (Creating a Factory!)

Now that we have our product, a framework for our factory, and hopefully a little funding, we can start producing our teddy bears (might be in pretty big debt though >:)

Here's our teddy bear factory and an instantiation of this factory. Then we will call the production function to start producing teddy bears and make some profit >:) Let's say our factory makes 5 bears a day. We'll also make a method to calculate the profit of all the functions we have and instantiate an ArrayList to keep track of how many TeddyBears we have.

public class TeddyBearFactory extends Factory{ //Creates child class for TeddyBearFactory and constructor w/ super() method
  static ArrayList<TeddyBear> teddyBears = new ArrayList<TeddyBear>(); //List collection of all the TeddyBear objects, static so universal list belonging to class
  public TeddyBearFactory(String owner){
    super(owner);
  }

  public void production(int days){ //Production of teddyBears over a certain number of days
    for(int i = 0; i < days*5; i++){ //For loop that repeatedly calls createBrownTeddyBear() and adds to static list
      TeddyBear teddyBear = TeddyBear.createBrownTeddyBear();
      teddyBears.add(teddyBear);
    }
  }

  public static void calculateProfit(){//Function to calculate universal profit among any given amount of teddy bear factories
    for (int i = 0; i < teddyBears.size(); i++){ //For loop
      factoryProfit += (teddyBears.get(i).price - teddyBears.get(i).cost); //Accesses static arraylist belonging to TeddyBearFactory and each object's properties
    }
  }
}

Main Method (Production)

Now that we have the building blocks to open our first toy factory, let's create (instantiate) one and get it running!

public class Main{
  public static void main(String[] args){
    TeddyBearFactory teddyBearFactory0 = new TeddyBearFactory("You"); //Instantiates our very first TeddyBearFactory

    teddyBearFactory0.production(3); //Calls production() method of our new factory with the argument of 3 days passed in
    TeddyBearFactory.calculateProfit(); //Calculates profit of our new factory after 3 days of production
    
    System.out.println("Here's how many Brown Teddy Bears we've produced: " + TeddyBearFactory.teddyBears.size());
    System.out.println("Here's our profit over three days: " + TeddyBearFactory.factoryProfit); 
  }
}

Main.main(null);
Here's how many Brown Teddy Bears we've produced: 15
Here's our profit over three days: 75.0

More Production

Let's open some new factories for some more sweet profit >:) We'll count our total profit from the previous three days and the next 3 days.

public class Main{
  public static void main(String[] args){
    TeddyBearFactory teddyBearFactory0 = new TeddyBearFactory("You"); //instantiates our very first TeddyBearFactory
    TeddyBearFactory teddyBearFactory1 = new TeddyBearFactory("You"); //instantiates our a second TeddyBearFactory
    TeddyBearFactory teddyBearFactory2 = new TeddyBearFactory("You"); //instantiates a third TeddyBearFactory

    teddyBearFactory0.production(3); //Calls production() method of our new factory with the argument of 3 days passed in

    teddyBearFactory1.production(3); //Calls production() method of our new factory with the argument of 3 days passed in
    
    teddyBearFactory2.production(3); //Calls production() method of our new factory with the argument of 3 days passed in
    TeddyBearFactory.calculateProfit(); //Calculates profit of our new factory after 3 days of production
    
    System.out.println("Here's how many Brown Teddy Bears we've produced with all our factories: " + TeddyBearFactory.teddyBears.size());
    System.out.println("Here's our profit: " + TeddyBearFactory.factoryProfit);

  }
}

Main.main(null);
Here's how many Brown Teddy Bears we've produced with all our factories: 60
Here's our profit: 375.0

Takeaways

Object Oriented Programming is quite powerful. It allows programmer to create archetypes through the concept of classes which they can then expand upon through inheritance. Additionally, it's abstraction of programming into objects allow code to be organized and modeled after the real world.

In my example, I used a factory to represent that classes act as a factory (slightly redundant). Every non-static method or variable is a blueprint within them that serve as instructions to produce a certain good (object) with its own functions and properties. Every static method or variable is a function or property of the factory itself which the factory can call upon.

Constructors are the initial instructions that are run in order to instantiate objects. This is the bulk of the factory's (class's) blueprint to create goods (objects). Sometimes this factory can also cause an object to run methods in when it's instantiating the object. For instance, an iPhone goes through quality control before it is packaged and sent as a final product.

Classes can also instantiate themselves so long as the constructor is called. In a factory example, a factory (class) producing electricity (object) can also use the electricity to power a toaster in the factory (method using electricity). This is one of the many examples of how unlike procedural programming which usually follows a set path of code, OOP enables programmers to utilize code from anywhere even outside the main() function. This leads into the topic of abstraction which I'll talk about sometime in the future.

All in all, OOP is pretty cool!