The basics of Java you need to know
Java is one of the most important programming languages in the world, and it's widely used by large companies. You need to know these concepts if you want to work with Java.
TL;DR
| Concept | Definition |
|---|---|
| Encapsulation | Hide the internal state. Reveal only what is necessary. |
| Abstraction | Reveal the what, hide the how. |
| Inheritance | Reusable code, inherited attributes and behavior. |
| Polymorphism | Same method but with distinct behavior according to the object. |
| Data Structures | Most used data structures (ArrayList, LinkedList, TreeSet, HashSet, HashMap). |
| Abstract classes | Classes that cannot be instantiated directly. |
| Strings | Working with text. |
| Exceptions | Error handling. |
| Generics | Work with different data types. |
| Optional | Handle null values more safely. |
What is OOP?
This is a paradigm of object-oriented programming. Objects can have attributes and behaviours. This makes the code more modular, reusable, and maintainable. There are four key principles:
Encapsulation
Hide the internal state. Reveal only what is necessary. Other objects can't access an object's data directly. Instead, you need to use a specific method for this.
public class Account {
private double balance; // Private
public void deposit(double balance) {
if (balance > 0) this.balance += balance;
}
public double getBalance() {
return balance;
}
}Abstraction
Reveal the what, hide the how. Don't import the implementation. For example, Notifier doesn't know if it's an email, SMS, or something else.
public interface Notifier {
void send(String message);
}Inheritance
Reusable code, inherited attributes and behavior. Classes can inherit from a parent class so you don't have to write the same code twice.
public class Employee {
protected double baseSalary;
public double calculateSalary() {
return baseSalary; // Base behavior
}
}
public class SalesPerson extends Employee {
private double commissions;
@Override
public double calculateSalary() {
return baseSalary + commissions; // Reuses baseSalary
}
}SalesPerson reuses the baseSalary field from the parent and extends the calculation with its own logic.
Polymorphism
Same method but with distinct behavior according to the object.
Animal a1 = new Cat();
Animal a2 = new Dog();
a1.makeSound(); // "Miau"
a2.makeSound(); // "Guau"There are two types: at compilation time (overloading) and execution time (overriding).
Overloading
Same method name, but different parameters. The compiler decides which one to use depending on the parameters. Resolved at compile time.
public class Calculator {
public int sum(int a, int b) { return a + b; }
public double sum(double a, double b) { return a + b; }
public int sum(int a, int b, int c) { return a + b + c; }
}Overriding
Same method but a subclass overrides it. It is decided at runtime based on the object's actual type.
Animal animal = new Dog(); // Variable of type Animal, actual Dog object
animal.makeSound(); // Execute the Dog versionThis works thanks to upcasting, which allows you to assign a Dog to an Animal variable because Dog extends Animal.

Data Structures in Java
| Name | Feature |
|---|---|
ArrayList | Quick access by index (O(1)). |
LinkedList | Fast insertion/deletion at both ends. |
HashSet | No duplicates, unsorted. |
TreeSet | No duplicates, sorted. |
HashMap | Key-value, unique keys, unsorted. |
I recommend you use HackerRank for solving data structure challenges. And you can write your code in the RPCIDE editor.
Abstract classes
These are classes that cannot be instantiated directly. An abstract class is like a base template.
Abstract class vs Interface
Abstract class for things that "are of the same" type. Interface for things that "can do" the same thing, without being of the same type.
Abstract class
public abstract class Shape {
public abstract double calculateArea();
}
// Here Square and Circle are shapes.
public class Square extends Shape {
private double side;
public Square(double side) { this.side = side; }
@Override
public double calculateArea() { return side * side; }
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) { this.radio = radius; }
@Override
public double calculateArea() { return Math.PI * radius * radius; }
}Interface
public interface Drawable {
void draw();
}
// Button isn't a Shape, but you can draw it.
public class Square extends Shape implements Drawable {
private double side;
public Cuadrado(double side) { this.side = side; }
@Override
public double calculateArea() { return side * side; }
@Override
public void draw() { System.out.println("Drawing square..."); }
}
public class Button implements Drawable {
@Override
public void draw() { System.out.println("Drawing button..."); }
}Strings
It is the most used data type. You should know the following methods for working with strings:
| Method | Function |
|---|---|
| length() | Returns the number of characters. |
| toUpperCase() | Converts the text to uppercase. |
| toLowerCase() | Converts the text to lowercase. |
| substring() | Extracts a specific part of the text. |
| contains() | Checks if the text contains a specific sequence. |
| equals() | Compares if two texts are identical. |
| trim() | Removes empty spaces at the beginning and the end. |
| split() | Divides the text into an array using a separator. |
| replace() | Swaps characters or words for something else. |
Important: String == String doesn't compare content correctly in many cases. You'd better use .equals().
Exceptions
Exceptions are events that occur during the execution of a program and interrupt the normal flow of the code. An exception is an object that represents an unexpected problem.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("You cannot divide by zero");
} finally {
System.out.println("This always runs");
}Handling exceptions using try/catch ensures that the programme does not crash abruptly.
Exceptions follow the hierarchy set out below, and you can find out more about this topic at Class Exception.

Generics
Generics <> are a way to write classes, interfaces and methods that let you work with different data types.
List<String> list = new ArrayList<>(); // Allow only String type objects
lista.add("Andres");
lista.add("Parra");
String text = list.get(0);We can create our own generic class:
public class Entity<T> { // <T> is a generic type. It can be String, Long, etc.
T getId();
void setId(T id);
}Generic types are mainly useful for two things:
- Detect errors at compile time.
- Create reusable code.
Optional
Optional is a container that may or may not hold a value, and it helps you safely handle cases where the value could be missing.
public String getBillingCity(String idUser) {
return findUserById(idUser)
.orElse("CITY NOT AVAILABLE");
}If the value returned by findUserById is null, the value will be the one in the orElse.
You can read this post to find out more about the Optional.
Keep learning
Although AI already programmes very well, even better than many developers, it is important not to forget these concepts. They are not going away, and it remains vital to understand them to get the most out of artificial intelligence. Keep training your mind, you can use RPCIDE to constantly work through small coding exercises.