This article is meant as a brief and high-level introduction to OOP and what it means to be an OO developer. The things explained in this article (and most other articles) are meant to be looked at on the principle level, try to understand what's being conveyed instead of focusing on syntax.

Putting it all Together

Let's look at a complete example with everything we know so far:

// This is the class definition, it defines what a Logger object looks like.
class Logger {
    // The log file, a property that describes which log *this* object logs to.
    String logFile;

    // The log method, a method that defines *how* logging is done.
    void log(String message) { 
        // Write the current time plus the message to the logFile.
        writeToFile(logFile, getTimestamp() + " " + message);

// Somewhere else

//Create a new Logger object as defined in the Logger class.
Logger logger1 = new Logger(); 
//And *another* one. This is a completely different logger.
Logger logger2 = new Logger(); 

// Define the state for each logger seperately
logger1.logFile = "/var/log/error.log";
logger2.logFile = "./debug.log";

logger1.log("There was an error!");
logger2.log("I did a thing!");

Notice how I call the same method (log) on both files. Both calls will do the same thing, they will log a line to a file. But each would log the line to a different file, as specified in the logFile property we set beforehand.


That's the basic idea. Create encapsulated object with behavior and state, and use those in your programs. It might not seem as useful now, but in future articles we would explore how object oriented programming affects and improves the way we think about our programs.

A massive thank you to Madara Uchiha, who I borrowed this article from, check out his site at: