Introduction to Object-Oriented Programming

ยท

4 min read

Prerequisites

  • You have set up a Java IDE. If you haven't, here's how to set one up.
  • You're interested in learning something new ๐Ÿค—๐Ÿค—.

Object-Oriented Programming (OOP) is a term popular in tech. In this article series, I'll be guiding you through the concepts and components of Object-Oriented Programming such as:

  • abstraction
  • encapsulation
  • inheritance, and
  • polymorphism

I will be using the Java programming language to illustrate the concepts.

OOP is a programming technique that centres around thinking of code as objects(entities) with identities, attributes, and behaviours. Simply put, an object has a name, a defined set of attributes/characteristics, and behaviours or actions the object can perform. In classical coding, objects are instances of classes and a class is the blueprint that contains all attributes and behaviours of an object.

To illustrate the basics, an object can be a dog. And naturally, a dog has a name, colour of its fur, and breed. These are the attributes of the dog. It is also a living creature, so it can run, bark, sleep, etc. We can call these the behaviours or actions that can be performed by the dog.

In programming, attributes are used by assigning variables in the class while behaviours are functions/methods(blocks of code used to perform a particular task) written in the said class. To show this in code, we'll be writing a 'Dog' class in Java with attributes 'name', 'furColour' and 'breed'. Going with general guidelines, a Java file is supposed to contain only one class to avoid ambiguity. Here's the code.

class Dog{
    String name;
    String furColour;
    String breed;
}

The attributes 'name' and 'furColour' are written in the string type (types would be discussed in a later article).

Common actions/behaviours performed by dogs include:

  • jumping
  • running
  • barking

We're going to implement those methods. Here's how to do it,

class Dog{
    String name;
    String furColour;
    String breed;

    public void bark(){
        System.out.println(name + " is barking...");
    }

    public void jump(){
        System.out.println(name + " is jumping...");
    }

    public void run(){
        System.out.println(name + " is running...");
    }

    public String toString(){
        return ("Dog name: " + name + "\n" + "Dog fur colour: " + 
        furColour + "\n" + "Dog breed: " + breed);
    }
}

we use System.out.println(argument) to print the input argument on the console

There are three methods in the class. These methods are the class behaviours, as I have already explained. We have the script for the class, but how do we use it? I said earlier, a class is a blueprint.

To create an instance of our class(an object), we create a variable of the type 'Dog'. We call the compiler to create a new instance of the class using the new keyword, followed by the class_name, and a parenthesis. Next, we define its attributes. Simply put, give values to its attributes. To do this we create a new Java file named Run and in the file create a class named run which would have a main method(this is the method executed by the compiler), follow the code below.

class Run{

    public static void main(String[] args){
        Dog bingo = new Dog();
        bingo.name = "Bingo";
        bingo.furColour = "Brown";
        bingo.breed = "Dalmatian";
        bingo.bark(); 
        Dog foo = new Dog();
        foo.name = "foo";
        foo.breed = "Pug";
        foo.run();
        System.out.println("=============================="); // acts as a separator
        System.out.println(bingo.toString());
        System.out.println("==============================");
        System.out.println(bingo.toString());
    }
}

we call object behaviours using object.behaviour_name(required method argument(s))

we access its attribute using object.attribute_name

In the above snippet, we created two objects of the class Dog, assigned names to them, fur colours and breeds. We also called one of their behaviours. We notice the object named foo doesn't have a colour assigned to it and in the output code it is assigned null. This is because the default values assigned to variables/attributes when they are created. Java uses null as the default case for strings, zero for integers(numbers) and false for booleans(variables with two values, true or false). The result returned is seen below.

Result output printed in the console

This is just a basic demonstration and I would encourage changing other attributes, adding new behaviours and running the script to see the results. Let your imaginations run free. A class can be as complex as you want, but you need to follow some guidelines to make your classes consistent and easy to modify for the future.

This article on the 'Introduction to Object-Oriented Programming' is the first of many. The consecutive articles would shed more light on the concepts of Object-Oriented Programming such as abstraction, inheritance, and polymorphism.