A program is nothing but a set of step-by-step instructions that only a computer can understand so that it can come up with a solution. There are different approaches to do that, which in technical term, are referred to as programming paradigms. .
Different paradigms for problem solving Imperative Programming Structured/Procedural Programming Object Oriented Programming Declarative Programming Logic programming paradigm Functional programming
Imperative Programming Control flow in imperative programming is explicit : commands show how the computation takes place, step by step. Each step affects the global state of the computation. The paradigm consist of several statements and after execution of all the result is stored.
result = [] i = 0 start: numPeople = length(people) if i >= numPeople goto finished p = people[ i ] nameLength = length(p.name) if nameLength <= 5 goto nextOne upperName = toUpper (p.name) addToList (result, upperName ) nextOne : i = i + 1 goto start finished: return sort(result)
Advantage: Very simple to implement It contains loops, variables etc. Disadvantage: Complex problem cannot be solved Less efficient and less productive Parallel programming is not possible
Structured Programming Structured programming is a kind of imperative programming where control flow is defined by nested loops, conditionals, and subroutines, rather than via goto’s . Variables are generally local to blocks. Early languages emphasizing structured programming: Algol 60, PL/I, Algol 68, Pascal, C, Ada 83, Modula, Modula-2.
result = []; for i = 0; i < length(people); i ++ { p = people[ i ]; if length(p.name)) > 5 { addToList (result, toUpper (p.name)); } } return sort(result);
Object Oriented Programming The program is written as a collection of classes and object which are meant for communication. The smallest and basic entity is object and all kind of computation is performed on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of real life problems which are today in scenario. C++, Java, C#, python, Ruby etc.
Advantages: Data security Inheritance Code reusability Flexible and abstraction is also present
The important features of object–oriented programming are Bottom–up approach in program design Programs organized around objects, grouped in classes Focus on data with methods to operate upon object’s data Interaction between objects through functions Reusability of design through creation of new classes by adding features to existing classes
Declarative programming paradigm It is divided as Logic, Functional, Database. In computer science the declarative programming is a style of building programs that expresses logic of computation without talking about its control flow. It often considers programs as theories of some logic. It may simplify writing parallel programs. The focus is on what needs to be done rather how it should be done basically emphasize on what code code is actually doing. It just declare the result we want rather how it has be produced. This is the only difference between imperative (how to do) and declarative (what to do) programming paradigms.
Logic programming paradigm It can be termed as abstract model of computation. It would solve logical problems like puzzles, series etc. In logic programming we have a knowledge base which we know before and along with the question and knowledge base which is given to machine, it produces result. In logical programming the main emphasize is on knowledge base and the problem. The execution of the program is very much like proof of mathematical statement, e.g., Prolog
sum of two number in prolog: predicates sumoftwonumber(integer, integer) clauses sum(0, 0). sum(n, r):- n1=n-1, sum(n1, r1), r=r1+n
Functional programming paradigms The functional programming paradigms has its roots in mathematics and it is language independent. The key principal of this paradigms is the execution of series of mathematical functions. The central model for the abstraction is the function which are meant for some specific computation and not the data structure. Data are loosely coupled to functions.The function hide their implementation. Some of the languages like perl , javascript mostly uses this paradigm.
Need for object-oriented paradigm it produces reusable code/objects because of encapsulation and inheritance. the data is protected because it can be altered only by the encapsulated methods. it is more efficient to write programs which use pre-defined objects. the storage structure and/or procedures within an object type could be altered if required without affecting programs that make use of that object type. new functions can easily be added to objects by using inheritance the code produced is likely to contain fewer errors because pretested objects are being used. less maintenance effort will be required by the developer because objects can be reused.
Differences between OOP and POP
Overview of OOP concepts Class Objects Inheritance Polymorphism Abstraction Encapsulation
Class The class is a group of similar entities. It is only an logical component and not the physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.
Object An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object contains both the data and the function, which operates on the data. For example - chair, bike, marker, pen, table, car, etc.
Inheritance Inheritance is an OOPS concept in which one object acquires the properties and behaviours of the parent object. It’s creating a parent-child relationship between two classes. It offers robust and natural mechanism for organizing and structure of any software.
Polymorphism Polymorphism refers to the ability of a variable, object or function to take on multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptop , a foot race , and business . Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.
Abstraction An abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.
Encapsulation Encapsulation is an OOP technique of wrapping the data and code. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example - in school, a student cannot exist without a class.