What is Object-Oriented Programming (OOP)?


Knowing the basics of Object-Oriented programming will help you a lot when learning how to code in an OOP language. I'll explain to you in simple terms how an OOP language works.



Transcript

Object-Oriented programming (OOP) is exactly like the name says. It is programming that focusses on Objects. Let me explain it in simpler terms, in the way I have come to understand and think about OOP.

In OOP, you see everything as objects that can interact with each other. Look at an object-oriented program as its own virtual world. The characters are objects that can interact with each other. As an example, look at the animated movie, ‘Wreck-It Ralph’. In this movie, you have characters of old arcade games interacting with each other. They also interact with their game worlds. Object Orientation works the same. It is a way of making abstract concepts more concrete.

In procedural programming, you have functions that manipulate data. With OOP, you have objects that interact with each other in certain ways. They have pre-programmed ways to interact with each other and with their environment.

An object can have attributes or properties. They give more details about the object. Then it has methods that specify how it interacts with its attributes and other object.

To explain it in a more concrete way, I decided to pull out my daughter’s lego to use as an example.

For example a lego figurine, consists of a head, hair, upper body and lower body. These can be of different colours and facial expressions. This will be its attributes. Then it can have a method that will tell it how to ‘build’ itself to look like an actual lego figurine. It can also have other methods as to how to put a saddle on the lego horse for example.

In Java, the lego figurine will look as follows:

            public class LegoFigurine {
    private String name;
    private Head head;
    private HeadCovering hairHat;
    private UpperBody shirt;
    private LowerBody legs;

    public void build() {
        // Method to show legoFigurine how to put itself together
    }
}
        

Head, HeadCovering, UpperBody and LowerBody are all other classes that LegoFigurine consists of. This would classify a 'has-a' relationship. As in LegoFigurine has a Head, for example.

An Object-oriented language mainly consists of classes interacting with each other, by calling specific methods on those classes to manipulate the data in them. Even the standard library classes in Java works on the same principle. Here is an example program in Java that adds Strings to a list and then loops through the list to print the values on the console.

            public class SimpleApp {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("My");
        list.add("name");
        list.add("is");
        list.add("Liezel");
        list.add("Shaw.");
        for (String listItem : list) {
            System.out.print(listItem);
            System.out.print(" ");
        }
    }
}
            
        

An ArrayList is an object which extends List and is able to hold a 'list' of multiple objects of any type as long as it extends Object. All objects in Java ultimately extends Object. Firstly, we create a new ArrayList and call it list. Then we add a bunch 'String' objects to it, which you specify with quotes. After that we loop through each item in the list and print them out on the screen.

This gives you a bit more of an idea what an Object-Oriented language looks like and how it works. If you have any questions, or comments for me, please leave a comment below and let me know.



Leave a comment

Your email address will not be published

Master Your Mindset:
10 Coding Myths dispelled

 

If you are interested in learning to code but you are not sure if you are cut out to be a Software Developer, click on the link below to get this free resource to put your mind at ease and dispel some of these common myths.