Programming Paradigms refer to a way of solving problems using the features available in a language. In other words, the style of programming
Imperative programming paradigm
This uses the imperative mood (making a command or request). It uses a series of commands, which specify what the computer has to do – and when – in order to achieve a desired result. E.g, Java, Basic, Fortran, Assembler, etc
Declarative programming paradigm
This focuses more on what a program should accomplish and less on ‘how’ to go about it. Programs describe their desired results without explicitly listing commands that must be performed. It may seem like ‘magic’ but there’s a lot of work going on under the hood.
An example is furniture assembly: While imperative programming provides instructions for assembly, declarative programming provides a picture of the finished piece of furniture as a template.
Most modern languages support both paradigms to an extent.
Procedural programming
Procedural programming follows a linear, step-by-step approach to creating software. Each procedure is usually a function or a set of functions. Languages that follow procedural programming concepts include Pascal, FORTRAN, etc
An application to use a library, when written in procedural programming format would look somewhat like this:
Advantages of Procedural Programming
Downsides to Procedural Programming
Object Oriented Programming
In real life, a lot of things are related and share similar properties. All dogs bark, everybody eats. OOP structures a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects
Classes often represent broad categories, like Car or Dog that share attributes. These classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object.
Classes can also contain functions, called methods available only to objects of that type.
For example, say we created a class, Dog, to contain all the properties a dog must have, name and color. We then create an instance of a Dog type object, myDog to represent my dog.
We could then set the value of the properties defined in the class to describe my dog, without affecting other objects or the class template.
We can then reuse this class to represent any number of dogs. The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.
Encapsulation
This principle states that all important information is contained inside an object and only select information is exposed. The implementation and state of each object are privately held inside a defined class.Â
Other objects do not have access to this class or the authority to make changes. They are only able to call a list of public functions or methods.
Abstraction
Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button. Applying abstraction means that each object should only expose a high-level mechanism for using it.
This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.
Preferably, this mechanism should be easy to use and should rarely change over time. Think of it as a small set of public methods which any other class can call without “knowing†how they work.
Inheritance
Objects are often very similar. They share common logic. But they’re not entirely the same. So how do we reuse the common logic and extract the unique logic into a separate class?
One way to achieve this is inheritance.For example, consider a (parent) class Person, that has methods walk and talk. Chris is an object created from class Person but Chris is a teacher. Now a teacher is a person but not every person is a teacher. We can create a (child) Teacher from Person, add a method introduce to it, and then create the object Chris from Teacher.
Therefore the common logic remains in Person, which is inherited by Teacher and is therefore present in chris.
When we call chris.walk(), the walk() method moves up the chain of child to parent classes, to find where the walk method is defined.
This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy.
Polymorphism (Many-forms)
Polymorphism gives a way to use a class exactly like its parent so there’s no confusion with mixing types. But each child class keeps its own methods as they are. Polymorphism allows different types of objects to pass through the same interface. (shapes class, circle, rectangle, etc)
Assume we have classes Circle and Square derived from class Shape, and class Shape as a method calcArea for calculating area of the shape, we can add different implementations of the getArea method to each inherited class and which will then return the area for that particular class. The method can take many forms.
Advantages of OOP
Criticisms of OOP