For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
Programming paradigms are essential methods for structuring and organizing code, providing developers with a framework to tackle problems using particular techniques and styles, which ultimately results in more maintainable and flexible programs.
Grasping various software development paradigms is essential as it enhances problem-solving abilities, boosts code maintainability, and enables more efficient and flexible solutions.
The core element of procedural programming is functions while OOP bases its structure on objects and Functional Programming focuses on functions as main citizens with emphasis on data immutability and reduction of side effects.
Ready to level up your Java skills? Our Software Development courses are designed to push you to the next level — start your journey today!
The following article explains multiple dominant programming paradigms, including their fundamental aspects of OOP vs Functional vs Procedural.
The fundamental approach of program development exists as a method or technique for programming that affords programmers organizational models to approach issue resolution. The programming paradigm explains how programs should be arranged and controlled, as well as outlines data handling processes alongside data modification rules. Most development approaches utilize unique approaches to software development, requiring a basic grasp to choose the correct strategy for individual challenges.
There exist three major programming paradigms, which include:
Procedural Programming bases its operation on procedures known as functions or routines that are sections of executable code dedicated to perform distinct tasks. The methods have to be performed one after the other in a sequential order. The top-down approach defines this method since function calls and data manipulation handle the program execution sequence.
Objects as fundamental units of programming are obtained through classes in Object-Oriented Programming. Every class serves as an object template that merges properties (attributes) with functionalities (methods). Object-oriented programming constructs code by using real-world entities that manage both object states and behaviors independently.
Computational tasks in Functional Programming arise from evaluating mathematical functions as the fundamental concept. FP refrains from altering state and mutable data, focusing instead on the application of pure functions. It’s founded on the principle that functions ought to be first-class citizens and that programs can be built by combining smaller functions.
Logic programming
Logic programming is a framework grounded in formal logic. In logic programming, the program is made up of a collection of facts and rules, to derive new facts or address problems through logical queries.
Declarative programming
Declarative programming emphasizes the goals of the program instead of the methods to achieve those goals. It removes the control flow details and emphasizes outlining the reasoning behind the computation.
The presence of various programming paradigms stems from the varied characteristics of computing challenges, the progression of programming languages, and the ongoing quest for solutions that are more efficient, maintainable, and scalable. Various paradigms offer distinct tools, abstractions, and methods for tackling particular kinds of challenges in software development. Let's examine some reasons for the existence of multiple paradigms and their significance.
Also Read: OOP vs POP: Difference Between OOP and POP
Procedural programming is a style where programs are organized as a series of instructions (functions or procedures) that are followed sequentially to address a problem. It highlights what the program accomplishes instead of how it achieves that.
Features:
Instances of Languages: C, Pascal, alongside Fortran are widely known procedural programming languages.
Advantages:
Disadvantages:
Example Code (Java):
public class AddNumbers {
// Define a method to calculate the sum of two numbers
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = add(num1, num2); // Call the add method
// Print the result
System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
}
}
Software design under Object-Oriented Programming (OOP) depends on objects instead of functions and logic for its organization, thus relying on encapsulation along with inheritance, abstraction, and polymorphism to create modular, maintainable, reusable code.
Features:
Advantages:
Disadvantages:
Example Code (Java):
// Define a class (blueprint for objects)
class Dog {
// Attributes
private String name;
private String breed;
// Constructor (initializes object's attributes)
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
// Method (action that an object can perform)
public void bark() {
System.out.println(name + " says Woof!");
}
// Getter methods to access private attributes
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
}
// Main class to create and use Dog objects
public class DogDemo {
public static void main(String[] args) {
// Create objects (instances of the class)
Dog dog1 = new Dog("Buddy", "Golden Retriever");
Dog dog2 = new Dog("Lucy", "Poodle");
// Access attributes and call methods
System.out.println(dog1.getName() + " is a " + dog1.getBreed());
dog1.bark();
System.out.println(dog2.getName() + " is a " + dog2.getBreed());
dog2.bark();
}
}
Also Read: What are the Advantages of Object-Oriented Programming?
Functional programming is a style that considers computation as the assessment of mathematical functions and refrains from altering states and mutable data. It highlights pure functions, immutability, and first-class functions, resulting in succinct, predictable, and simpler-to-understand code.
Features:
Advantages:
Disadvantages:
Example Code (Java):
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FunctionalVsImperative {
// Imperative approach (modifies the list in-place)
public static int[] addOneImperative(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] += 1;
}
return numbers;
}
// Functional approach (creates a new list and returns it)
public static List<Integer> addOneFunctional(List<Integer> numbers) {
return numbers.stream()
.map(x -> x + 1)
.collect(Collectors.toList());
}
public static void main(String[] args) {
// Example usage
int[] numbersArray = {1, 2, 3};
List<Integer> numbersList = Arrays.asList(1, 2, 3);
// Calling the imperative method
System.out.println("Imperative: " + Arrays.toString(addOneImperative(numbersArray)));
// Calling the functional method
System.out.println("Functional: " + addOneFunctional(numbersList));
}
}
The table below illustrates the feature comparison of OOP vs Functional vs Procedural in detail.
Feature | OOP | Functional | Procedural |
Code Structure | Organizing code around objects, which encapsulate data and behavior. | Organizing code around pure functions and immutable data. | Organizing code around procedures (functions) that operate on data. |
State Management | Objects maintain their own state, which can be modified by methods. | Avoids mutable state; prefers immutability and stateless functions. | The state is managed globally and is modified by procedures. |
Modularity & Reusability | High modularity through classes and objects. Inheritance, polymorphism, and composition allow for reusable components. | Modularity through functions that operate on data. Function composition and higher-order functions enable reusability. | Modularity through separate procedures that perform discrete tasks. Code reusability is achieved by calling functions with different parameters. |
Performance | It can be slower due to the overhead of object creation and inheritance. | Often efficient for stateless operations, but recursion may impact performance. | Can be very efficient due to direct manipulation of data. |
Real-world Use Cases | Large systems, GUIs, game development, and enterprise applications. | Mathematical computations, data transformations, and parallel processing. | System programming, scripts, low-level software, and quick tasks. |
Also Read: Software Design Patterns: A Complete Guide for 2025
Each programming paradigm—Procedural, Object-Oriented, and Functional—has its own strengths and ideal use cases. Understanding their real-world applications helps in choosing the right approach for different projects. Let's explore how each paradigm is applied in various scenarios.
Use Cases:
Applications:
Both Python and JavaScript languages support different programming paradigms yet they allow procedural programming as a solution for specific tasks.
Procedural programming offers efficient management of hardware and system resources.
This methodology works best for projects involving basic tasks that require linear processes throughout the process.
Numerous programming developers use procedural programming first because of its straightforward nature which serves as a foundation for understanding other paradigms.
Processes in programming can run again and again to simplify development time and maintenance tasks and resulting code becomes more efficient.
Due to its transparent command structure procedural programming enables simple and sequential code execution which enables better testing and debugging processes.
Software Engineering:
OOP plays an essential role when designing user-friendly interfaces that require interactive components.
Web applications become easier to develop and maintain through OOP because it allows for proper organization of their structure.
The development of strong, expandable mobile applications depends heavily on OOP concepts.
The representation of real-world objects together with their connections becomes possible through OOP in database systems.
Through Object-Oriented Programming, developers can establish advanced AI together with machine learning models and their related applications.
Through OOP, developers depict game elements such as characters and items as objects that contain specific attributes along with actions.
OOP allows developers to construct dynamic video game environments through its capabilities.
Applications:
The key aspects of client-server system designs, along with their implementations, rest on OOP principles.
Real-time applications benefit from OOP because it allows developers to create programs that need precise timing as well as swift responses.
Computer systems designed using OOP create simulated complex operational models that include manufacturing operations and economic modeling applications.
Programming systems with OOP lead to software development for office automation activities such as word processing and spreadsheet management.
OOP is important for creating systems that handle and showcase hypertext and hypermedia content.
OOP concepts are applied in the creation of neural networks and parallel processing systems.
OOP aids in creating expert systems that are capable of making decisions rooted in knowledge and guidelines.
OOP is utilized in creating e-commerce platforms and applications.
OOP is applied in creating systems for Computer-Integrated Manufacturing, Computer-Aided Design, and Computer-Aided Manufacturing.
Data Manipulation and Alteration:
Excessive data manipulation combined with complex transformations can be handled efficiently through functional programming because it allows for both safe and efficient data management.
Big data frameworks that use Apache Spark rely on languages including Scala and Clojure for fast and simple handling of big data sets.
A task with numerical calculations and simulation requirements benefits most from Functional programming because of its need for reliable and repeatable processes.
Concurrency and Parallel Execution:
Through its emphasis on immutability together with pure functions the creation of concurrent and parallel applications becomes simpler thus minimizing race conditions while improving application performance.
The development of fault-tolerant distributed systems employs Erlang as a target language because it was created specifically for concurrency and fault tolerance.
Modern web frameworks like React/Redux implement functional programming theory to properly handle state data during the development of scalable applications.
Web Design:
Concepts from functional programming, such as immutability and pure functions, help create user interfaces that are simpler to test and maintain.
Functional programming can be utilized to develop resilient and scalable APIs and services.
The syntax and functional requirements of specific problem domains originate through functional programming principles when designing Domain Specific Languages (DSLs).
Additional Uses:
Financial domains implement functional programming because of its need for exact results through pure functions and the advantage of referential transparency.
Definitions of financial systems also apply to functional programming because of its emphasis on pure functions and immutability, which enables secure, dependable system creation.
Programming in a functional style functions perfectly in artificial intelligence sectors, including natural language processing and machine learning, because pattern recognition, together with symbolic processing, forms the core foundation.
The implementation of functional programming increases the simplicity of testing and controlling game development logic.
The functional programming elements find their place in compiler implementations because code demands transformation and optimization processes.
From Procedural Programming to Object-Oriented Programming
A substantial reorganization occurs when moving your code structure from Procedural Programming (PP) to Object-Oriented Programming (OOP). Procedural programming relies on functions along with procedures to work with data through a common state but OOP organizes code by uniting data with behavior under single programmable entities.
Steps for Transitioning from Procedural to Object-Oriented Programming:
The data organization in procedural programming occurs primarily through arrays, along with lists, dictionaries, or tuples.
The transition to OOP requires grouping data elements that have connections into one object structure. Your system-based objects should directly represent tangible entities such as BankAccount or Car or Person.
Each procedure in procedural programming functions autonomously with the data supplied to each function.
Within OOP coding, you transform each procedural function into class-specific methods which operate on features of the class (like self.balance).
You should shift your program's data storage from global space, including variables and dictionaries, into objects that unite substance and operational actions. Better data control and integrity exist because all object modifications need to pass through the methods related to the object.
Multiple entities with similar characteristics and behaviors should consider inheritance since it reduces duplication of code. The inheritance principle enables you to create a base class named Account to support SavingsAccount and CheckingAccount types of accounts.
Functions that manipulate data structures ought to be transformed into methods that function on class instances. This allows for encapsulation and a neater design.
From OOP to Functional
Moving from Object-Oriented Programming (OOP) to Functional Programming (FP) involves a change in your perspective on and organization of your code. OOP organizes code around objects that combine data and behavior, whereas FP highlights functions, immutability, and declarative programming that minimizes side effects and mutable states.
Steps for Moving from OOP to FP:
In OOP, objects frequently encapsulate data. In functional programming, data is usually represented as unchangeable data structures (e.g., tuples, lists, maps).
Recognize entities and represent them as data structures instead of objects.
In OOP, objects frequently possess a changeable state. In FP, you'll steer clear of changing state. Rather, utilize functions that produce new data structures containing modified values.
For instance, rather than using task.complete(), you would utilize a function that yields a new task reflecting the modified state (task.replace(completed=True)).
In OOP, functions work with data contained in objects. In FP, you would substitute these methods with pure functions that accept data as input and provide transformed data as output.
For instance, rather than using taskManager.add_task(task), you would send the task to a function that generates a new task list.
The implementation of inheritance characterizes the regular utilization patterns in OOP for function enhancement. Function composition serves as a replacement for inheritance in functional programming to generate new behaviors.
Through higher-order functions, you can modify as well as improve existing functions.
You can achieve sophisticated behavior through the combination of basic reusable functions in FP. You can establish a collection of functions that take care of workflow data processing needs.
The majority of object-oriented and procedural programming uses loops as standard features. Functional programming includes an alternative approach to repetitive operations through recursion which functions as methods that execute themselves with different values passed in.
Languages Supporting Multiple Paradigms:
Modern programming languages provide various paradigms as built-in features to help developers solve different application problems using the best approach found in a single programming language. Various programming techniques operate within a single language project or codebase through these languages, which support Multiple Programming Paradigms, including OOP, FP, and Procedural Programming (among others).
The following list includes some famous programming languages with multiple supported paradigms:
Paradigms: Object-oriented, Functional, Procedure-based
Python emerges as a flexible language that supports OOP functionality and also enables both FP and procedural programming features. The Python language allows you to build object-oriented programs using classes and objects, and through functional code with higher-order functions, while creating procedural logic by using basic functions together with loops.
Main Attributes:
Models: OOP, Functional, Procedural
JavaScript serves multiple programming paradigms because it provides broad adaptability to all three paradigms. To implement your code, you have the flexibility to build object-oriented classes with prototyping while adopting functional programming design patterns using higher-order functions and closures alongside a procedural approach through functions and loops.
Main Characteristics:
Paradigms: Object-Oriented, Functional, Procedural
Ruby is mainly created with a focus on object-oriented programming, while also accommodating functional and procedural programming approaches. It is recognized for its clarity and ease of use.
Main Characteristics:
Paradigms: OOP, Functional
Scala is a combined language that merges Object-Oriented Programming and Functional Programming. It enables you to create both object-oriented and functional code, and its architecture enables developers to utilize the finest aspects of both approaches.
Main Characteristics:
Paradigms: Object-Oriented Programming, Procedural Programming, Generic Programming
C++ is a multi-paradigm language that facilitates OOP, procedural, and also generic programming through templates. It’s commonly utilized in applications that are critical to system and performance.
Main Characteristics:
Paradigms: Mainly Functional, yet accommodates certain OOP concepts
Haskell is a strictly functional programming language, yet recent advancements have permitted certain object-oriented patterns (through type classes, for instance). It is commonly utilized for tasks that require heavy data processing, where immutable characteristics and mathematical integrity are crucial.
Main Characteristics:
Paradigms: Functional, Object-Oriented, Procedural
F# is a functional-priority language that operates on the .NET framework. Nonetheless, it accommodates both functional programming and object-oriented programming models, classifying it as a multi-paradigm language.
Main Characteristics:
Also Read: Top 20 Programming Languages of the Future
Combining OOP and Functional approaches (code)
Merging Object-Oriented Programming (OOP) with Functional Programming (FP) can capitalize on the advantages of each approach: OOP's organization and structure, along with FP's emphasis on immutability, pure functions, and higher-order functions.
Here’s a Java example that illustrates how the two paradigms can be integrated:
An uncomplicated utilization of OOP and FP in Java.
We will establish a class to signify Person entities. Next, we will employ functional programming methods such as higher-order functions and immutability.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Person {
private final String name;
private final int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Method to greet
public String greet() {
return "Hello, my name is " + name + " and I am " + age + " years old.";
}
// Functional method to increase age (immutability)
public Person increaseAge() {
return new Person(this.name, this.age + 1);
}
// Functional method to check if a person is an adult
public boolean isAdult() {
return this.age >= 18;
}
}
public class OOPAndFunctional {
public static void main(String[] args) {
// Create a list of Person objects
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 15),
new Person("Charlie", 22),
new Person("Diana", 17)
);
// Functional approach: Increase age using map() and filter adults using filter()
List<Person> adults = people.stream()
.map(Person::increaseAge) // Increase age
.filter(Person::isAdult) // Keep only adults
.collect(Collectors.toList());
// Display results
adults.forEach(person -> System.out.println(person.greet()));
}
}
Breakdown of the Code:
Output:
Hello, my name is Alice and I am 31 years old.
Hello, my name is Charlie and I am 23 years old.
Q: What distinguishes immutability in Functional Programming from mutable data in Procedural and Object-Oriented Programming?
A: Functional Programming: Data is unchangeable, indicating that once it is created, it cannot be modified. This aids in avoiding issues associated with side effects and simplifies understanding the code.
Procedural and Object-Oriented Programming: Data is generally changeable, indicating it can be altered directly. Although this approach can offer greater flexibility, it might result in unforeseen side effects, complicating the maintenance and debugging of the code.
Q: Could you clarify the idea of "state" in OOP, FP, and PP?
A: OOP: The state is usually kept in the properties of objects. Entities can alter their internal state via methods.
FP: The state is usually unchanging. Functions do not modify the state but generate new values derived from the inputs. This indicates that there is no internal condition to modify.
The state is held in variables that can be changed by functions. PP frequently depends on the concept of a global state that is utilized throughout various sections of the program.
Q: What is the functioning of inheritance in OOP, and why is it not applied in Functional Programming?
A: Inheritance in OOP enables a class (child) to acquire attributes and methods from a different class (parent). This aids in reusing code and can establish hierarchies of interconnected objects.
Functional Programming circumvents inheritance since FP prioritizes composition over inheritance. Rather than establishing hierarchies of objects, FP promotes the development of smaller, reusable functions that can be combined to create more intricate behavior.
Q: Can you provide an instance when you would choose Procedural Programming instead of OOP or FP?
A: Procedural Programming is an excellent option for small, straightforward programs that require minimal complexity or state handling. For instance, a script that handles data or executes a sequence of steps might gain from the clarity and straightforwardness of procedural programming.
It's also useful in performance-sensitive applications where object overhead or recursion in functional programming could add unwarranted complexity.
Q: What are the drawbacks or difficulties associated with Object-Oriented Programming?
A: Complexity: OOP may result in over-engineering, particularly in smaller projects where the complete functionality of classes and inheritance may not be required.
Performance: Utilizing objects and dynamic dispatch (invoking methods on objects) can occasionally lead to added overhead, particularly in applications where performance is crucial.
Tight coupling: Although OOP encourages reusability, poor design may result in tight coupling among classes, complicating maintenance.
Also Read: Most Common OOPS Interview Questions & Answers
Transforming a procedural program into an object-oriented program (OOP) requires recognizing functions and data within the procedural program, then structuring them into objects that have attributes (data) and methods (functions). Let's go through the procedure using an example.
Example of a Procedural Program:
Let's examine a basic procedural program that handles a bank account, where we execute actions such as depositing, withdrawing, and checking the balance.
// Procedural approach to managing a bank account
public class BankProcedural {
static int accountBalance = 1000; // Global variable for account balance
public static void deposit(int amount) {
accountBalance += amount;
System.out.println("Deposited " + amount + ". New balance: " + accountBalance);
}
public static void withdraw(int amount) {
if (amount <= accountBalance) {
accountBalance -= amount;
System.out.println("Withdrew " + amount + ". New balance: " + accountBalance);
} else {
System.out.println("Insufficient balance!");
}
}
public static void checkBalance() {
System.out.println("Current balance: " + accountBalance);
}
public static void main(String[] args) {
deposit(500);
withdraw(200);
checkBalance();
}
}
Object-Oriented Program Conversion:
To transform this into an object-oriented program, we will establish a BankAccount class. The account balance will transform into an attribute of the object, while the deposit, withdraw, and check balance actions will serve as methods of the class.
// Object-Oriented approach to managing a bank account
class BankAccount {
private int balance; // Encapsulated balance variable
// Constructor to initialize balance
public BankAccount(int initialBalance) {
this.balance = initialBalance;
}
// Method to deposit money
public void deposit(int amount) {
balance += amount;
System.out.println("Deposited " + amount + ". New balance: " + balance);
}
// Method to withdraw money
public void withdraw(int amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrew " + amount + ". New balance: " + balance);
} else {
System.out.println("Insufficient balance!");
}
}
// Method to check balance
public void checkBalance() {
System.out.println("Current balance: " + balance);
}
}
// Driver class
public class BankOOP {
public static void main(String[] args) {
// Creating a BankAccount object with an initial balance
BankAccount account = new BankAccount(1000);
account.deposit(500);
account.withdraw(200);
account.checkBalance();
}
}
Key Changes from Procedural to OOP in Java
Encapsulation: The balance is now an attribute of the BankAccount class rather than a global variable, keeping the state contained within the object.
A whole functional method requires development to find the Fibonacci sequence through simple steps.
The functional method employs strict rules against variable states, side effects, along imperative iterations during execution. The solution will consist of input-output functions that solve problems through recursive computation methods.
Issue: Compute the Fibonacci Series
The Fibonacci sequence consists of numerical figures that result from adding the immediately preceding two numbers while starting from zero and one.
Fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) for n > 1
Pure Functional Solution (using recursion):
We use recursion to find Fibonacci numbers, while the technique represents a "pure functional" approach since it operates without mutating any variables.
Implementation in Java (Functional Programming):
// Pure functional approach to compute Fibonacci numbers
public class FunctionalFibonacci {
// Recursive function to compute Fibonacci
public static int fibonacci(int n) {
// Base cases
if (n == 0) return 0;
if (n == 1) return 1;
// Recursive case
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
// Calculate the first 10 Fibonacci numbers
for (int i = 0; i < 10; i++) {
System.out.println("Fibonacci(" + i + ") = " + fibonacci(i));
}
}
}
Explanation of the Code
Characteristics of Pure Functional Programming
Output:
Fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(2) = 1
Fibonacci(3) = 2
Fibonacci(4) = 3
Fibonacci(5) = 5
Fibonacci(6) = 8
Fibonacci(7) = 13
Fibonacci(8) = 21
Fibonacci(9) = 34
We need to write a program using Object-Oriented Programming (OOP) and Functional Programming strategies which can operate together in one codebase.
An intuitive program will monitor the library books through our development. The application implements Book objects for book representation while functional programming elements filter the book collection through specific criteria like post-year releases.
Issue: Library Administration System
We will model a book with the OOP method (as a Book class).
We will oversee a selection of books in a library through a different OOP class (Library).
We will employ functional programming to execute specific tasks, like filtering books according to publication year or organizing them.
A Book class contains all the attributes that describe one book, which include the title and the author, and the publication date.
The Library class controls a book collection alongside functions for book additions and listing complete book catalogs.
The relevant tasks receive a Functional Programming implementation at this stage.
Using map and filter, and lambda functions, we will control book selection along with modification based on release year requirements.
Code Implementation:
import java.util.*;
import java.util.stream.Collectors;
// OOP Approach: Book class
class Book {
private String title;
private String author;
private int publicationYear;
// Constructor
public Book(String title, String author, int publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
// Getter methods
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPublicationYear() {
return publicationYear;
}
// String representation
@Override
public String toString() {
return "'" + title + "' by " + author + ", Published in " + publicationYear;
}
}
// OOP Approach: Library class
class Library {
private List<Book> books;
// Constructor
public Library() {
this.books = new ArrayList<>();
}
// Add book to library
public void addBook(Book book) {
books.add(book);
}
// List all books
public void listBooks() {
books.forEach(System.out::println);
}
// Functional Approach: Filtering books by year using stream & lambda
public List<Book> filterBooksByYear(int year) {
return books.stream()
.filter(book -> book.getPublicationYear() > year)
.collect(Collectors.toList());
}
// Functional Approach: Sorting books by title using stream & lambda
public List<Book> sortBooksByTitle() {
return books.stream()
.sorted(Comparator.comparing(Book::getTitle))
.collect(Collectors.toList());
}
}
// Main Class
public class LibrarySystem {
public static void main(String[] args) {
// Creating books using OOP
Book book1 = new Book("The Catcher in the Rye", "J.D. Salinger", 1951);
Book book2 = new Book("1984", "George Orwell", 1949);
Book book3 = new Book("To Kill a Mockingbird", "Harper Lee", 1960);
Book book4 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
// Using Library class (OOP)
Library library = new Library();
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
library.addBook(book4);
// Listing all books (OOP)
System.out.println("All Books in Library:");
library.listBooks();
// Filtering books published after 1950 (Functional Approach)
System.out.println("\nBooks Published After 1950:");
List<Book> filteredBooks = library.filterBooksByYear(1950);
filteredBooks.forEach(System.out::println);
// Sorting books by title (Functional Approach)
System.out.println("\nBooks Sorted by Title:");
List<Book> sortedBooks = library.sortBooksByTitle();
sortedBooks.forEach(System.out::println);
}
}
Explanation
OOP Approach
Functional Approach (Using Java Streams)
Output:
All Books in Library:
'The Catcher in the Rye' by J.D. Salinger, Published in 1951
'1984' by George Orwell, Published in 1949
'To Kill a Mockingbird' by Harper Lee, Published in 1960
'The Great Gatsby' by F. Scott Fitzgerald, Published in 1925
Books Published After 1950:
'The Catcher in the Rye' by J.D. Salinger, Published in 1951
'To Kill a Mockingbird' by Harper Lee, Published in 1960
Books Sorted by Title:
'1984' by George Orwell, Published in 1949
'The Catcher in the Rye' by J.D. Salinger, Published in 1951
'The Great Gatsby' by F. Scott Fitzgerald, Published in 1925
'To Kill a Mockingbird' by Harper Lee, Published in 1960
Are you prepared to immerse yourself in the realm of programming? Discover upGrad's extensive courses to acquire practical skills in Object-Oriented Programming (OOP), Functional Programming, and Procedural Programming. Whether you’re starting out or aiming to enhance your abilities, upGrad provides expert-led courses that equip you with the tools and assurance to excel in these fundamental programming paradigms.
Object-oriented programming (OOP) allows you to structure code into classes and objects, enhancing code modularity, reusability, and maintainability. Functional Programming highlights functions as primary entities and advocates for immutability and declarative coding, enhancing scalability while minimizing side effects.
Procedural Programming emphasizes creating code in the form of a series of instructions or procedures, which is best suited for straightforward tasks and allows for greater control over the flow of the program.
By signing up for upGrad’s programs, you will gain insights into the theoretical frameworks of these paradigms while also engaging in practical projects to reinforce your comprehension. Acquire essential practical experience that will prepare you to be a flexible programmer, equipped to face any challenges in the technology sector!
Begin your journey with upGrad's courses today and enhance your programming abilities!
Reach out to the nearest upGrad centre for more information.
Similar Reads:
For large-scale applications, Object-Oriented Programming (OOP) is often considered the most effective paradigm due to its ability to promote modularity, reusability, and maintainability through the use of classes and objects.
Yes, a project can and often should use multiple programming paradigms, leveraging the strengths of each for different parts of the project. This is known as multi-paradigm programming, and it can lead to more efficient, readable, and maintainable code.
No, functional programming is not always better than object-oriented programming (OOP); both paradigms have strengths and weaknesses suited for different types of problems and projects. The best choice depends on the specific context and requirements of the project.
Yes, you can absolutely use multiple programming paradigms within a single project, and it's often a beneficial approach, allowing you to leverage the strengths of different paradigms for specific tasks.
Switching programming paradigms mid-project is a complex undertaking, but can be done by first understanding the reasons for the change, then analyzing the existing codebase, and finally, refactoring and adapting the code to the new paradigm.
Common misconceptions about programming paradigms include the belief that one paradigm is inherently superior, that you must stick to only one, or that following rules rigidly is necessary. Misconceptions also include the idea that object-oriented programming (OOP) is just about using classes, or that Agile means no documentation.
Multi-paradigm programming languages, like Python, Java, C++, and JavaScript, support multiple programming styles or paradigms, such as object-oriented, functional, and imperative programming, offering flexibility in problem-solving.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.