top of page
Writer's pictureMiss S

An Introduction to Object Oriented Programming

Updated: Jul 10, 2023

Imagine a world where, instead of having one, long, continuous program filled with different functions, you could take it one step further and split your program into different, mini programs that each do their own thing, Where instead of a program assembled of procedures, there were interoperable objects that can communicate with one another to make a larger program.


When learning to program, we all learn how to create procedural programs. The world we have just imagined is that of object-oriented programming.


Object-oriented programming is where inter-connected objects known as classes come together to make up a larger program. There are many programming languages that can use object-oriented techniques, but the one that this guide is going to focus on is Java. Object-oriented programming has three essential components: objects, classes and methods.


Methods

We have already come across methods in previous programming methods. Methods are functions and procedures and are used to run the actual elements of each class. Every class will need a constructor method. These methods are used to facilitate the creation of a class. ’Getters’ and ‘Setters’ are also essential methods in our classes. These will be explained later.


Classes

Classes are a collection of methods and variables. Each class should have its own particular use, and there are different kinds of classes, all used to create objects at runtime. Regular ’bog-standard’ classes can sometimes be known as a concrete class; they are just classes that have their certain purpose.


Objects

Objects are classes that are created at runtime, that are then used to run the program. If a class does not have a constructor method, then no object will be constructed and the object will not be made.


Our first class


Public class HelloWorld() {
    private String name;
    
    //This is the constructor of the class, note that it has the 
    //same name and capitalisation of the class name. This shows
    //that it is the constructor. This will be ran whenever the 
    //class is created. Notice the javadoc used
    
    /**
     * Constructor HelloWorld
     * @param name - the name of the user
     */
    public HelloWorld(String name) { 
        this.name  = name;
        sayHelloWorld(name);   
    }
    
    //This method is used to write hello world and the user’s name
    //to the console window. This will run whenever the class is 
    //created as it is called in the constructor.
    //name, string, the user’s name
    
    /**
     * function sayHelloWorld - writes hello world to the screen
     * @param name - the name of the user
     */
    public void sayHelloWorld(String name) {
        System.out.println(“Hello world! Hello “ + name);
    }
}//end of class HelloWorld

The class above shows some very important things. Firstly, how constructors work. When we create a class, capitalisation is super important. In class declarations, all class names should begin with a capitol letter, and any additional word should also begin with a capital letter. In this case, ours is HelloWorld (imaginative, I know).


This class also shows how JavaDoc works. JavaDoc is Java’s standard documentation of classes and objects. It is denoted by a /** followed by lines of * and ending with */. JavaDoc commenting should include the name of the function or class, and any parameters included in the function declaration. To declare a parameter you should use @param [name] [what the parameter is for]. This helps your JavaDoc to be clear to the reader and makes your code much more readable. It also allows some IDEs to create HTML versions of for you, without you needing to intervene.


Another important thing to note, is the difference between private and public methods. Private methods are methods that cannot be accessed from outside of this class. They are only available in the class and any classes related to this class will not be able to access it. Public methods can be accessed from any class and can be used within other parts of the program, once the classes are inter related. It is good practice to ensure that all variables are private, to prevent unneeded or unwanted access or change to the values stored invariables.


Watch out for the next guide on object oriented programming which will focus on getters, setters and imports.


Food for thought

If all of the variables’ access is set to private how might we change the value in a variable, or get the value of a variable. Hint: all will be revealed in the next article.



Recent Posts

See All

Records

A brief introduction to the record data structure.

Comments


bottom of page