Programming Paradigms
What is the programming paradigms? Since the beginning of software development, there have been many approaches to designing programming languages. The concepts, principles and rules of a programming language is called a programming paradigm. Though in theory, programming languages fall under one paradigm only. In practice, a programming language be a combination of multiple paradigms.
Many modern programming languages have following one or more programming paradigms. The list is not exhaustive, but only lists major paradigms.
- Imperative Programming
- Object Oriented Programming
- Declarative Programming
- Functional Programming
Imperative Programming
Imperative Programming is a programming paradigm in which statements are written to change the state of the program. The name imperative implies the fact that the instructions that are executed dictate how the program executes.
An imperative program can give un-deterministic results for same inputs as each input can change the state of the program.
For example:
The counter class declares the count attribute and the state of the object changes as we call the addValue
method.
class Counter {
// declare count attribute
private int count;
// constructor
public Counter(int initVal) {
this.count = initVal;
}
// method updates count state
public void addValue(int value) {
count = count + value;
}
// count getter
public int getCount() {
return count;
}
}
class Main {
public static void main(String[] args) {
// instantiate the counter
Counter counter = new Counter(0);
// add 10 to the counter the state changes from 0 to 10
counter.addValue(10);
System.out.println("current value: " + counter.getCount());
// add 10 to the counter the state changes from 10 to 30
counter.addValue(20);
System.out.println("current value: " + counter.getCount());
// add 10 to the counter the state changes from 30 to 60
counter.addValue(30);
System.out.println("current value: " + counter.getCount());
}
}
Output:
current value: 10
current value: 30
current value: 60
Declarative Programming
As opposed to the imperative programming, the declarative programming is programming paradigm that specifies what program should, without specifying how to do it.
SQL is an example for declarative programming. In SQL, operations on database entities are declared, and the database management systems takes care of execution.
SELECT invoice, count(line_items)
FROM purchases
GROUP BY invoice;
Functional Programming
Functional Programming paradigms are like mathematical functions. They do not alter the state of the program and results deterministic output for a give input.
Static functions and lambda functions are examples of functional programming. They do not alter the state of the object of application, but only process the inputs and returns the output.
Object Oriented Paradigm
Object Oriented Programming extends the imperative programming paradigm. In a good deal of modern programming languages, imperative and functional programming languages can co-exist. Java, Python are lively examples of imperative and functional paradigm coexistence.
Object Oriented Programming is very matured paradigm and various programming languages are based on it. It is important to understand the concepts and principles of OOP.
Read more about Object-Oriented Programming
Objects and Classes
Classes and objects are building blocks of object-oriented programming. A class is a template or a blueprint to create an object. For example, Car is a class and Ford Ecosport is an object.
Classes and objects encapsulate the attributes and behavior like car has engine, steering and can move forward and backward.
Object-oriented programming is based on four fundamental principles
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Read more about Classes and objects
Encapsulation
Encapsulation in object-oriented programming is an ability to bind the attributes and behavior. The idea is to keep attributes and behavior in place so that the classes can be maintained and extended easily. Encapsulation also provides mechanism to hide the unnecessary details from the user using access specifiers like private, protected.
Encapsulation helps in decoupling the entities and modules in java. Decoupled entities or modules intern helps in maintaining and extending functionality without affecting one another.
The technique through which decoupled components (classes/modules/code) are changed internally without affecting their exposed external behavior is called code refactoring.
Abstraction
Abstraction is closely related to encapsulation and to some extent it overlaps with encapsulation. Abstraction provides a mechanism to expose what an object and hides how an object does what it’s supposed to do.
A real world example is a car. User does not need to know how car engine works and torque gets generated to move the car. Instead, he just needs to know, it moves with accelerator.
Inheritance
Inheritance is the ability to base an object or class on another one. Top level class is called parent class or super class, and the classes extends the attributes and behavior of parent class are child classes or subclasses.
For example, if Vehicle
is a parent class and Car
, Truck
are subclasses which extends Vehicle
class, Car
and Truck
classes inherits the public and protected attributes and methods from Vehicle
class. Following code can elaborate the example,
Vehicle.java
class Vehicle {
// number on vehicle plate
public String number;
// constructor
public Vehicle(String number) {
this.number = number;
}
// drive method
public void drive() {
System.out.println("drive!");
}
}
Car.java
class Car extends Vehicle{
// constructor
public Car(String number) {
// call vehicle constructor
super(number);
}
}
Truck.java
class Truck extends Vehicle{
// constructor
public Truck(String number) {
// call vehicle constructor
super(number);
}
}
Client.java
class Client {
public void process() {
// create car object
Vehicle car = new Car("IN00RT2011");
// call drive method. drive method is inherited from vehicle class
System.out.println("Vehicle Number: " + car.number);
car.drive();
}
}
Output:
Vehicle Number: IN00RT2011
drive!
In the above example, Car
class and Truck
class inherit the attribute number
and drive()
from Vehicle
class
Read more about Inheritance In Java
Polymorphism
Polymorphism gives an option to use the interface for entities of different types. There are two types of polymorphism
Runtime polymorphism is also known as dynamic polymorphism and subtype polymorphism. Runtime polymorphism comes into play when a subclass inherits a superclass and overrides its methods. In this case, compiler cannot decide which methods will be executed from super class and subclass implementation. So, the decision is made at runtime.
Complie time polymorphism is also known as static polymorphism. Compile-time polymorphism is used for method overload. For an example we have shape class, and it has two methods with same name getArea
but different number of parameters. getArea(float radius)
to calculate the area of circle and getArea(float length, float breadth)
to calculate the area of rectangle. Here the compiler can decide which method to bind based on the number of parameters.
Read more about Polymorphism In Java