Basic Concepts of OOP
Nowadays, almost every modern programming language is object-oriented. This article I will talk about basic and important concepts of OOP, which are need at projects and at most of job interviews either.
I will use JAVA as the programming language and IntelliJ IDEA as the IDE.
The concepts I will describe;
- OOP
- Object
- Class
- Access Modifiers
- Field
- Method
- Constructor
- “new” keyword
- “this” keyword
- getters /setters methods
Object-Oriented Programming
OOP is a type of programming that is driven by modeling your code around building units called objects. Each object represents a real-life object in the world.
Technically objects are a kind of variable. But they are not primitive variables such as integer, double, etc. So objects are stronger variables in comparison to others.
For example; let’s define a variable that corresponds to a car in the real world.
As you can see, the box on top is our model (variable). And we can create more than one instance.
Classes
Classes and Objects are two different terms and should not be used interchangeably, they can sometimes seem like they both refer to the same thing but each has a different meaning.
A class;
- is a Data type
- has its own file with the extension “.java”
- defines the structure
- starts with an upper case
An object;
- is a variable
- has no file and can be used around the project
- used to implement logic
- starts with a lower case
Now let’s talk about how we can design our object in the code-world. Firstly we create a class named MyCar:
Field
And now let’s add the features to our car:
The features are named in Java as fields. They can be named as “attributes” or “member variables” in other languages.
in our example; power, color, and seats are fields of the model (class).
Access Modifiers
We see the keywords “public” and “private” beginning of the fields and also class. These are access modifiers.
You always have the final call on which fields you’d want to make public vs private, and it always depends on the purpose of the field as well as the overall design of your code.
We can add functions to the class as well. And we call these functions “methods”.
Constructor
Constructors are special types of methods that are responsible for creating and initializing an object of that class.
When you create a constructor, careful with two things;
i) Constructors don’t have any return types
ii) Constructors have the same name as the class itself
Constructors can have parameters and it is possible to create more than one for an object.
Speaking for our example, let’s define a constructor called MyCar for the class. And we’d like to make sure that any object of type Mycar will start with the productionYear value set to 2020. Therefore I change the method.
Now let’s create an object of class MyCar
. For this in Main
class (a special class in Java) firstly I create a sample of the object and call the method that calculates the age of the car.
“new” Keyword
As you see, we don’t use .
(dot) to create an object, but we use new
keyword.
Mycar myCar = new MyCar()
This will create an object called myCar
using the default constructor. And by this object, you can call the method in Class.
“this” Keyword
When we need to refer to an object within one of its methods or constructors, to do so we use the keyword this
.
this
is a reference to the current object (the object whose method or constructor is being called). We can refer to any field of the current object from within a method or a constructor by using this
.
In image-5 we refer to the field productionYear by using this
within the method ageOfTheCar and also within the constructor.
MyCar(){
this.productionYear = 2020;
}
public int ageOfTheCar(int thisYear){
return thisYear - this.productionYear;
}
getters / setters methods
We have mentioned above about access modifiers. The advised way to label a field of a class is to make it private, so not public. And instead, creating public methods that provide a way to get their values is better. These methods are called accessor methods or getters.
And similarly, it’s better to create public methods that provide a way to set or update the values of the fields. And we call them mutator methods or setters.
By convention, getters start with the word “get” and setters with the word “set”. And then comes the method name.
This way, you can be sure that sensitive fields are not accessible and not changeable anywhere else in the project. And it is only possible to get or set/change them by using getters and setters. It is less risky and more explicit.
And it looks like below: