Java OOP : What are the Four Pillars?

Nathan Chiche
NewTechTips
Published in
5 min readDec 15, 2022

--

Java is an object oriented based programming language. It only started supporting functional programming in 2014 with Java 8. Java’s full potential isn’t realized if you haven’t explored OOP and only used learned using functional approaches.

Object-oriented programming (OOP) is extremely relevant, and we will explore the 4 biggest topics not only within Java but also in other OOP languages like Javascript.

I’ve always had issues with these concepts myself so i hope this article will help you on your journey.

OOPS concepts:

  1. Class
  2. Object
  3. Method
  4. Four Pillars

Getting familiar with classes, objects, and methods is highly recommended if you haven’t already.

We’ll now discuss the four pillars that make up Object-Oriented Programming:

  • Encapsulation(also Getters/Setters)
  • Inheritance (Parent and child relationship)
  • Abstraction(Interface and abstract classes)
  • Polymorphism (also overloading/overriding)

Encapsulation

In Java, encapsulation is the mechanism of wrapping the data (variables) and the code that manipulates them (methods) together as a single unit. The process of hiding data in a class using private and showing the methods with public

  • Utilizes and emphasises the use of access modifiers(control the visibility of fields, methods, and constructors in a class.)

We also use Setters and Getters to access and modify the data in a controlled manner, without being able to directly access the private variables

public class Person {
// Private variables that cannot be accessed directly
private String username;
private String password;

// Public methods that can be used to access and modify the private variables
public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public void setAge(int age) {
this.age = age;
}

public int getAge() {
return this.age;
}
}

In this example, the Person class contains private variables for the username and password of a person, as well as public methods for setting and getting the values of these variables.

Why is this important in the real world?

Encapsulation emphasizes software security. It helps root out unintended vulnerabilities in data breaches in which the attacker can gain access to unauthorized access to an objects internal data for example an individual’s username and password.

Inheritance

Inheritance is a way for the child class to reuse and extend the items of its parent class. The child class inherits the attributes and behaviors of the parent class, and can also have its own attributes and behaviors. This allows the child class to have the same capabilities as the parent class, and also to add new features to those that it inherits.

  • A child receives methods from parent class
  • Uses the extends keyword

A Vehicle can take on many diffrent forms such as a bike, scooter, or car. The main component they can all agree that they use is either they are moving or they are stopped which we can include in the parent class

void go() method prints out vehicle moving

void stop() method prints out vehicle stopped

We can extend this base class to any other child classes such that adding battery percentage to a electric scooter, or maybe a Miles-to-Empty guage on a car, and many more different features within their own child class

// Parent class
public class Vehicle {
void go() {
System.out.println("we are accelerating.");
}

void stop() {
System.out.println("We are stopping.");
}
}

// Child class
public class electricScooter extends Vehicle {

public void toggleBatteryPercentage() {
if (this.batteryPercentageOn) {
this.batteryPercentageOn = false;
} else {
this.batteryPercentageOn = true;
}
}
}

// Create a electricScooter object
electricScooter Razor = new electricScooter();

Razor.stop();
Razor.toggleBatteryPercentage();

Polymorphism

Polymorphism is a confusing concept but actually quite simple. Imagine a single function that adds two numbers. There are many data types such as: Ints, Floats, etc. How would you go about it so that we can use one function to add all data types? Well we use polymorphism. All of these data types can be added together. This flexibility allows for more concise and modular code, and can make it easier to write and maintain complex programs.

  • Practically a child inheriting a method but being able to change

Assume there is a base class named Shape from which the subclasses Triangle and Circle, are derived.

Shape class has a method called calculateArea(),which is inherited by all subclasses. when the calculateArea()method is called in an object of the Triangle class, the method might respond by displaying the area of the Triangle. On the other hand, when the same method is called in an object of the Circle class, the circle’s area might be calculated and displayed on the screen.

Below is an example of how polymorphism is used

class Shapes {
public void area() {
System.out.println("The formula for area of ");
}
}
class Triangle extends Shapes {
public void area() {
System.out.println("Triangle is ½ * base * height ");
}
}
class Circle extends Shapes {
public void area() {
System.out.println("Circle is 3.14 * radius * radius ");
}
}
class Main {
public static void main(String[] args) {
Shapes myShape = new Shapes(); // Create a Shapes object
Shapes myTriangle = new Triangle(); // Create a Triangle object
Shapes myCircle = new Circle(); // Create a Circle object
myShape.area();
myTriangle.area();
myShape.area();
myCircle.area();
}
}

Abstraction

Java abstraction hides the details of how a class is implemented from its user. Therefore, a class’s user only needs to know what it does, not how it does it.

An interface is a collection of abstract methods that must be implemented by any class that implements the interface.

We use the abstract keyword

Classes that are abstract can’t be instantiated, but they can be subclassed. What does this mean?

The code below doesn’t work because we can’t use an abstract class we can only use it to help make regular classes

abstract class Dog{
public void bark(){
System.out.println("bark");
}
}
public class Abstraction{
public static void main(string[] args){
Dog d = new Dog();
d.bark();
}

}

The code below works since its been subclassed

abstract class Dog{
public void bark(){
System.out.println("bark");
}
}

class chihuahua extends Dog{}

public class Abstraction{
public static void main(string[] args){
Chihuahua c = new Chihuahua();
d.bark();
}
}

Method overriding vs overloading

Method overloading and overriding are two completely different concepts. and has to do with types of polymorphism.

  • Static polymorphism is also known as compile time polymorphism.
  • Dynamic polymorphism is also known as runtime polymorphism.

--

--

Nathan Chiche
NewTechTips

Huge passion for software development, Data Analytics, Information Technology, and more.