Class creation with Java - An Introduction to Object Oriented Programming

5 min read ⋅ 169 views

Object Oriented programming (OOP) is a style of programming loved by many, hated by few. Once you understand it’s concepts, programming begins to feel a lot more ‘human’.

What does this mean?

Well OOP has allowed programmers to work with data and objects, rather than logic and actions. This has in many ways revolutionised programming and is one of the reasons writing code is a lot more popular than it used to be.

The most popular programming language in the world is Java and it’s no surprise to find out this language uses OOP. Although OOP has an awkward learning curve to overcome, once it’s concepts are understood many programmers tend to stick with it as their preference when writing programs.

A brief history of OOP

The first language considered to be Object Oriented was SIMULA. As the name suggests, it was used to create simulations. SIMULA didn’t actually use classes though so there is some debate as to whether it was a genuine OOP language.

Alan Kay thought a language like SIMULA was a great way for non-specialists to create graphics-oriented applications. Something necessary for a personal computer. He sold his vision to Xeroc Parc and together they created the Dynabook. The actual language used for programming the Dynabook was Smalltalk which still exists today but isn’t commercially used.

The 1970’s and 80’s was when OOP started to really gain momentum. It was integrated into the C language resulting in C++ and this language became the first widely used commercial OOP language. C++ is still immensely popular today and is used in a variety of different industries within technology.

In the 1990’s a group of developers at Sun created a simpler version of C++ called Java. This programming language is now the most popular in the world. One of the reasons for this is that it is platform independent. A Java program can be run on any computer with a Java Runtime Environment (JRE) installed. It is also the go to language for Android application development.

Key terms

Some of the key terms in OOP are classes, fields, methods, constructors and objects. These are essentially the building blocks when it comes to OOP applications. The main ones we will be focusing on in this article though are classes, constructors and objects.

Creating a class

So, let’s start with classes. A class acts like a template or blueprint. It stores variables, constructors and methods that are then applied to objects. To declare a class you have to use the ‘class’ keyword followed by a class name. These are followed by a set of curly braces {} and the rest of the syntax for that class goes inside those braces.

Here is a basic example:

Class Name {

// variables etc


It is important to note that by convention the first letter of the class name is uppercase and the rest of the letters are lowercase. If the class name consists of multiple words then the first letter of each word needs to be uppercase. For example AlternativeName.

The body of the class is populated with constructors, methods and fields. Having all these features inside a class is encapsulation. This is part of the brilliance of Object Oriented Programming as it allows us to program at a higher level of abstraction. We can focus on the classes and objects rather than the data structures and the functionality as seperate things.

Below is a screenshot of a Java class.

From this screenshot you can see three private variables. These are title, artist and label.

A private variable is only visible to the class it belongs to. You can also use public variables and protected variables. Public variables are visible to all classes and protected variables are visible to the class they belong to and any subclasses.

As you can see the private variables in this screenshot are all declared within the class but they are outside of the constructor which sits below. These are known as instance variables. These variables are created when an object is created using the keyword ‘new’. They are also destroyed if that object is destroyed. They are used by objects to store their states. In this case the instance variables have been given private access to hide visibility.

Underneath is the constructor for the class. Constructors are very important when creating classes and every class has one. If a constructor is not written for a class then the Java compiler builds one by default, but it’s best practice to simply write one yourself. Always be sure to give the constructor the same name as the class. It’s also important to point out that classes can have more than one constructor.

Inside the brackets of the constructor are parameters. Inside the body of the constructor, the values of these parameters get assigned to to the fields of the object.

When an object is created, the constructor is invoked. Objects are created from classes. For Java, the keyword ‘new’ is used to create new objects. When creating an object from a class there are three steps you need to follow. These are declaration, instantiation and initialisation.

Declaration means a variable declaration with a variable name with an object type.

Instantiation is the ‘new’ keyword used to create the object.

Initialisation is the following call to the constructor.

Here is an example of object creation.

This screenshot shows how to instantiate three Album objects. As you can see all of the above steps are present here. We have the variable declaration, the ‘new’ keyword and the call to the constructor. The data given here is passed to the constructor according to the parameters. The object is then created. Once this has happened the fields initialised by the constructor will have the values of the parameters passed to the constructor.

This concept can be confusing at first but after enough time it becomes second nature and extremely beneficial. Being able to relate code to the physical world has some very powerful advantages. It is best to play around with classes until you have a firm understanding as they are the fundamental to programming with Java. 



How To Structure a Python Project

Writing programs isn’t all about the code. Sure when it comes down to it, the code is what makes the functioning program...


Pipenv - First Impressions

Pipenv is an experimental tool with the purpose of bringing together different packaging tools in one place. It has been...


How To Write Tests For Python

An important part of development in any Python project is testing. Testing is used to help define the intent of your cod...