Fundamentals 8 min read

OOP vs Functional Programming in Java: Principles, Code Samples, and Comparative Analysis

This article explains the core principles of object‑oriented and functional programming, demonstrates each paradigm with Java code examples, and provides a detailed side‑by‑side comparison covering mutability, composition vs inheritance, side effects, concurrency, and readability.

FunTester
FunTester
FunTester
OOP vs Functional Programming in Java: Principles, Code Samples, and Comparative Analysis

Object‑Oriented Programming (OOP)

OOP revolves around objects, which are instances of classes that encapsulate data (attributes) and behavior (methods). Objects interact through method calls, mirroring real‑world relationships.

Key OOP Principles

Class – blueprint defining shared attributes and methods.

Object – concrete instance representing an entity.

Encapsulation – bundles data with its operations, restricting direct access.

Inheritance – enables a class to acquire properties and behavior from a parent class, promoting reuse.

Polymorphism – allows different objects to be treated uniformly through a common interface.

Java OOP Example

The following Java class demonstrates a simple OOP design for a User with a private score field and related operations.

/** * User class */
public class User {
    private double score;
    /** * Constructor with initial score */
    public User(double init) {
        this.score = init;
    }
    /** * Add to score */
    public void add(double num) {
        this.score += num;
    }
    /** * Subtract from score */
    public void minus(double num) {
        this.score -= num;
    }
    /** * Get current score */
    public double getScore() {
        return score;
    }
}

This class encapsulates the score attribute and provides methods to modify and retrieve it.

Functional Programming (FP)

FP treats computation as evaluation of mathematical functions, avoiding mutable state. Functions are first‑class citizens that accept pure inputs and produce predictable outputs without side effects.

Core FP Principles

Immutability – data cannot be altered after creation.

First‑class functions – functions can be assigned to variables, passed as arguments, and returned.

Referential transparency – a function’s result depends only on its arguments.

FP Features in Java 8

Lambda expressions – concise syntax for anonymous functions.

Functional interfaces – interfaces with a single abstract method to enable lambda usage.

Streams API – functional style processing of collections.

Java FP Example

The code below computes the sum of squares of even numbers from 1 to 10 using IntStream, lambda expressions, and the Streams API.

public static void main(String[] args) {
    int sum = IntStream.range(1, 10)
        .filter(i -> i % 2 == 0)
        .map(f -> f * f)
        .sum();
    System.out.println(sum);
}

The IntStream generates a sequence of integers, filters even numbers, squares each via a lambda, and aggregates the result.

Side‑by‑Side Comparison

Mutability

OOP often involves mutable objects whose state changes over time.

FP emphasizes immutability, avoiding state changes after data creation.

Composition vs Inheritance

OOP relies on class hierarchies and inheritance for reuse.

FP prefers composition of small functions rather than inheritance.

Side Effects

OOP code may produce side effects by altering external state.

FP aims to limit side effects to well‑defined boundaries, enhancing predictability.

Concurrency

FP’s immutable data and pure functions are naturally thread‑safe.

OOP concurrency requires careful management of shared mutable state.

Expressiveness & Readability

FP often yields concise, declarative code through higher‑order functions.

OOP provides clear abstractions and intuitive modeling of real‑world concepts.

Both paradigms offer valuable approaches to software development. The choice depends on project requirements, team preferences, and problem domain, and many modern codebases blend OOP and FP techniques to leverage the strengths of each.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javafunctional programmingProgramming ParadigmsComparisonOOP
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.