Object-Oriented Programming: Classes

Class is the main concept in Object-Oriented Programming. If you really want to understand and use object-oriented programming, you must master the concept of Class.

In this article I’ll answer the following questions:

  1. What is Object-Oriented Programming (OOP)?
  2. Which is the main concept in OOP?
  3. What are a data structure and an abstract data type (ADT)?
  4. What is a Class?
  5. Who are the authors I recommend to read about OOP?

To answer the first five questions, we will follow the book “Object-Oriented Software Construction” by Bertrand Meyer. You can find this book on amazon using this link. All the concepts related to OOP are very well explained in the aforementioned book.

Object-Oriented Programming

Object-Oriented Programming (OOP) is a software development paradigm. It states principles, rules, design methods to solve real-life problems using an object-oriented programming language.

It is a common mistake to assume that OOP is about a specific programming language (i.e. Java or Python).

Once you learn the principles of OOP, it is a very easy task to learn a new programming language.

There are several important concepts related to OOP; some of them will be explained below.

Bertrand Meyer stated that an Abstract Data Type (ADT) is a mathematical model of a data structure that specifies the type of data stored, the operations supported on them, and the types of parameters of the operations.  An ADT specifies what each operation does, but not how it does it.  Typically, an ADT can be implemented using one of many different data structures. 

Notice that abstraction is the main concept in programming. Here you can find more information on what abstraction is and why it is important in programming.

Typically, an ADT is composed by:





You can find an example in section 6.4 “Formalizing the specification”, pp. 129, of the Book Object-Oriented Software Construction by Bertrand Meyer.

We refer to Data structure as any method of organising a collection of data to allow it to be manipulated effectively.

Class is an abstract data type equipped with a possibly partial implementation. This is a formal definition enunciated by Meyer. In the next session, you will see an informal one with examples.

AnObject is aninstance of a class.

A rule of thumb when you use OOP for problem-solving is that you must always keep in mind that OOP is for modelling the real world (or a fictitious one), which is to describe behaviours through a programming language.

Main OOP Concept: Class

As Meyer states, objects are not the subject in Object-Oriented Programming.

What is the central concept of object technology?

Anyone can answer “object”. However, it is not the right answer. Objects are useful, but they are not new in programming. Ever since Cobol has had structures or Pascal has had records, humanity has had objects.

Objects remain important to describe the execution of an Object-Oriented system. But the basic notion, from which everything in object technology derives, is class. Here again, is the definition:

Abstract data types are a mathematical notion, suitable for the system specification stage (also called analysis). Because it introduces implementations, partial or total, the notion of class establishes the necessary link with software construction — design and implementation. A class is said to be effective if the implementation is total, deferred otherwise.

The text before has been fully taken from the book “Object-Oriented Software Construction” by Bertrand Meyer.  You are encouraged to read Chapter 7 from this book to get a better understanding of the Class concept.

The practical and informal approach to understanding the concept of Class

In Object-Oriented Programming, we use classes to represent concepts of the real world.

Because Object-Oriented Programming is used to model real-life situations, let’s go through some examples.

Imagine that we are asked to create a software that must store the information related to a book. The information available is title, author, and publisher.

From the text above, we can see that the main concept we are dealing with is book. From here, we say that Book is going to be a class.

On another hand, we must model the information available related to the book. In programming, we call this information attributes of the class. So, the class book will have three attributes: title, author, and publisher.

There are several mechanisms and theories about how to identify classes, in this article we are going to keep it simple just giving some examples without getting deeper into any theory. You should read those theories to grasp a better understanding. You can find more information in the book An Introduction to Object-Oriented Programming by Timothy Budd.

As you should know by now, programming is part of a bigger process: Software Development.

Usually, software development is done by a team. Each member of the team has a role and one of the roles is the programmer. Programmers get models, and then transform them into code. But as part of this process, programmers also must identify new classes. Sometimes they must modify the models to apply good programming practices and principles of the programming paradigm.

Because of this, it is of most importance that programmers learn how to identify classes using the description of a problem.

Let’s explore the following examples so you can get a better understating of what is this about.

Problem 1: Automobile manufacturer

Mercedes-Benz is a German automobile manufacturer; it is a very important employer and has around 10 000 employees. The company has a record of all the employees with the following information: full name, age, salary, and nationality. The company wants to develop a software that must include the following functionalities:

  • The total amount of money the company must pay to the employees.
  • How many employees have a given salary?
  • The average age of the employees.
  • The employee with the lowest salary.

As you can see, this is a problem that someone can ask you to solve. Imagine that you can solve a similar problem for the car dealer closest to your home, or your favourite cafeteria.

Our first task to solve this problem is to identify the classes that will help us in modelling this situation.

What are the main concepts present in the problem statement?

We can easily see that the concepts are:

  • Automobile manufacturer with the name Mercedes-Benz and a record of all employees.
  • Employee, with the information: full name, age, salary, and nationality.

Beginners can make the mistake of identifying Mercedes-Benz as one of the concepts present in the problem statement. But notice that it is just an attribute (characteristic) of the automobile manufacturer. Therefore, we don’t model it as a class, but as an attribute of a certain class.

Another part of the class is operations (or methods). Those operations are actions that one can carry on using the information (attributes) related to the specific class.

If you read again our problem, you will see that software must have certain functionalities. We must model those functionalities as operations of some classes.

How do we know in what class we must add a certain operation? It will always depend on what class is holding the information that we need to complete the operation.

For instance, we must calculate the “total amount of money the company must pay to the employees”. For that, we need to know how much money each employee earns. Where is that information? Well, when we identify the classes, we stated that the automobile manufacturer keeps a record of all employees. Therefore, it is in that class where we must model the operation.

The solution

To summarise, we define a class by defining three components: name, attributes, and operations.

Class name: Automobile Manufacturer

Attributes: Name (Mercedes-Benz), Employees (record of all employees)

Operations: Total amount of money, how many employees have a given salary? average age of the employees, and employee with the lowest salary.

Here we just defined our first class.

Later on, in this course, we will see a better way of designing the classes, as well as other details that we have to consider at the design stage.

The second class will be the class Employee.

Class name: Employee

Attributes: full name, age, salary, and nationality


In this case, we don’t any specific operation for this class. The main reason is that we design this class (for now) only to hold data.

In this type of class, we design operations just to return the values of attributes. So, we can have one operation per attribute, which its sole purpose will be to return the value of the attribute.

Let’s now study how to model another problem.

Problem 2

Lenovo is a PC company that creates and sells laptops. The company has a record of all the laptops that the company creates with the following information: (screen) size and price.

  • Implement the necessary methods (operations) for calculating the following:
    • The total amount of money the company will earn once it sells all the laptops.
    • The average price of the laptops.                                                      
    • How many laptops have a screen of 15 inches?       
    • The model of the cheapest laptop.

Our first step will be to identify the classes. Remember that classes have three components: name, attributes, and operations.

Must we model Lenovo as a class? Yes, sure we can. However, Lenovo is the name of the PC company. Therefore, the best way to model this is by defining, in our model, an attribute name for the company.

Class name: PC Company

Attributes: Name (Lenovo), laptops (record of all the laptops)

Operations: Total amount of money the company will earn once it sells all the laptops, average price of laptops, how many laptops have a 15-inch screen, and the model of the cheapest laptop.

We also have identified another concept from the problem statement, it is the concept of laptop. Therefore, we must define another class.

Class name: Laptop

Attributes: model, screen size, and price


In this case, for now, we will keep the operations empty. As in the previous problem, the purpose of this class is to hold the data related to a laptop. Therefore, the operations will be to return the value of each attribute.

Creating a class in Java

Let’s use the last class we identified to write it in Java: the class Laptop.

class Laptop{
    private int screenSize;
    private float price;
    Laptop(int size, float price){
        this.screenSize = size;
        this.price = price;

The previous code is the Java implementation for the class Laptop that we defined in the previous section. Let’s analyse each of the components of the class.

As mentioned in the previous section, a class have three components: name, attributes and operations.

In Java, we first write the keyword class, then we write the class name (that is why you see the first line class Laptop) and open bracket ({). Open bracket means that everything within the brackets ({…}) is within or belong to the class. Formally, it is the implementation of the class.

Next, we declare the attributes.

The keyword private is an access modifier. It sets the accessibility level of the attribute. Private means the attribute is only accesible within the class.

When no access modifier is specified, it is public. Public means it is accessible from outside the class. In the next lesson, you will learn what this exactly means and how to use it to solve problems.

Now, there is a concept that is very important in Object-Oriented Programming: constructor.

Every class should have a constructor. We understand by constructor a special method that is called anytime we create an object (next lesson) using the specific class. In OOP we use it to initialise the values of the attributes.

We use the parameter values of the constructor to initialise the class attributes. Most of the time, the implementation of a constructor is just to assign the values received as parameter to the attributes.

In the implementation of this constructor, notice we wrote “this.screenSize”. “this” is a keyword in Java that we use to refers (or access) to the attributes of the class. So, if we write “this.price”, the class must have the attribute price.

The next step is to implement the operations. Notice that in our case, we didn’t specify any operations.

So, we finish the implementation by closing the bracket (}) we open in the first line of code.

Class operations

Suppose that when the company sale a laptop, it has to add the VAT (15%) to the price of the laptop. This is a common practice in many countries.

So, our new operation should return 15 % of the price.

As a result, the implementation of the class Laptop will now look as follows.

class Laptop{
    private int screenSize;
    private float price;

    Laptop(int size, float price){
        this.screenSize = size;
        this.price = price;

    public float getVAT(){
        return this.price * 15 / 100;

In most programming languages, and specifically in Java, we use asterisk ( * ) for multiplication and slash ( / ) for division.

Syntaxis to define an operation in Java

Notice that the modifier for this operation is “public”. We want every “user” of this class to able to perform this operation.

Next, we define what is the type of the value we are going to return. We want to return the VAT, which is a percentage of a float number.

The result of a percentage operation is always a real number. Therefore, we specify that the method will return a float value.

After that, we specify the name of the operation (getVAT). You can use any name you want. As long as you follow the rules for variable names (don’t start with numbers, don’t use special characters, etc.

These rules can vary from one programming language to another).

Then, we have to specify the parameters for the operation.

Parameters are additional data that we need to perform the operation.

Let’s analyze this situation. What do we need to calculate the VAT?

We only need the price of the laptop because we already know that the percentage is 15%.

We can see that the price of the laptop is an attribute of the class.

For that reason, we don’t need any additional data to perform the operation getVAT. That’s why this method does not receive any parameters.

Now let’s analyze the constructor. The operation that the constructor must carry on is to initialize the class attributes.

So, the question here is with what values I can initialize the attributes?

We don’t know at this point. That’s why we use two parameters, to specify the values we want to assign to the attributes.

From now on, we are going to refer to operations as class methods, or simply methods.


By now, you should have a minimum understanding of what a class is in programming, and how to identify them from a problem statement.

Don’t get in a hurry to put this to work on a computer.

Next, we will start learning the basics of a specific programming language.


To get a deep understanding of the topics studied in this lesson, you should read the following: