OOPS concept 1
OOPS concept part1
OOPs is a programming paradigm. It is a type/way of writing code.
Some of the paradigms are:
- Procedural
- OOPs
- Functional
Procedural paradigm:
Set of some instructions or set of some functions. Each function internally call another function and program will start from very special function/procedure called as main.
In this process you need to call main function and it will call all other functions. main function is a trigger point of your program.
Organize code in set of functions and each functions can call another function and each program run from special function called main.
Issues:
- Not connected to real world - It cannot tell that if something is happining to someone. As it don't work on objects and their behaviour and don't help to work on their emotions.
- You cannot have flexibility in passing parameters to a function once function signature is defined.
- If have too many parameters to a function then cannot manage them and it will become messy.
- We can use struct to club all parameters into one single parameter. Struct cannot have methods in it.
- It make it hard to relatable in real life. As it say that something is happinging to someone. As it create a software which is not close to real life program.
- It will make hard to maintain the code as all functions will be present in the code and code will look bulky.
- This nature made procedural program paradigm far away from real life.
Disadvantage:
- difficult to make sense in big system.
- Entity is not tide-up with behaviour as in method parameter will not be checked on behaviour aspects.
- In procedural programming paradigm any function can call any function and that is an issue. There is no control.
- It will make hard to understand the code.
- It leads to spaghetti code.
OOPs paradigm:
It is a software paradigm which revolves around entities(special set of features).
Entities:
- Control its attributes.
- Define behavior associated to it.
- They are heart of your software system.
- It may have or not defines behavior.
- Class is an entity.
- students
- instructors
- batch
- course
- mentors
- exams
- placements
OOPs property or terms:
- Abstraction
- Polymorphism
- Encapsulation
- Inheritance
Abstraction is also known as principle of OOPs. Polymorphism, Encapsulation and Inheritance are pillars of OOPs.
Pillar means which will help to implement the principles or known as backbone of any rule or template or any work. In short pillar is also known as foundation.
Abstraction:
Hiding functionality. Representing software in terms of ideas.
Idea (entity) means anything in software system. It has two things attributes and associated behoviours.
It help to represent any idea to software world.
example: if we have a controller code which will call getAPI() method then getAPI() method will be knowing about the getData() method but not know how getData() method will do it.
So in short abstraction will represent the idea and will help you to know the flow of achiving the goal without distracting you to thing about how each flow will complete the task.
Purpose of abstraction:
No need for client to know internal details of achieving the goal. eg: DTO.
Encapsulation:
It is pillar of OOPs. It will help to implement abstraction. Wrapping (capsule) of something as a bundle so that your task can be done by the bundle without any issue.
Purpose of capsule:
- To hold contents together
- protect from outerworld.
Purpose of encapsulation:
- allows us to store attributes and behaviours together.
- allows us to protect attributes/methods from illegal acess.
Achieve encapsulation:
- Class
- entities
- behaviours
- Access modifiers to define class access levels.
- If we use private access modifier then that method or entity cannot be accessed outside the class.
Class:
Blueprint of any idea. Plan to achive any goal. It will only define your idea or work but will not implement it. Hence it don't take any memory. Class provide us opportunity to create multiple objects of same nature. Inshort a class can have n numbers of objects which will consume memory and will perform tasks defined in class.
Object:
It is a real life entity which will consume memory and will perform tasks defined in class.
Functional programming paradigm:
Process to bind every idea mathematically. It is a declarative style of coding.
It focus on what to solve not on how to solve(imperative style of coding). In it we use expressions instead of statements.
Expression executes to provide values. Statements execute to assign variables.
Concepts of functional programming:
- Pure functions
- Recursion
- Referential transparency
- Functions are First-Class and can be higher-order
- Variables are immutable
Pure Functions:
- Always produce same output for same input.
- They do not disturb or change global and local variables or input/output streams.
- Help to write parallel and concurrent applications.
- It help to parallelize the code, wait for result, memorize the result.
example:sum(x,y) return x+y
Recursion:It don't have for or while loop in short there is no itterable concept to have iteration facility coder need to use recursion.example:
fib(n) if(n<=1)
return 1;
else
return fib(n-1) + fib(n-2);
fib(n)
Referential Transparency:
Variable once defined don't change there values through the program. It do not have assignment operation we need to create new variable to store the result. It makes code safe as we can have all version of data in the code over different variables and can use them to reproduce the value if needed.
example:
x=x+1 is not a valid action as we can't modify the x variable value.
x=1
y=x+1 it is valid.
Functions are First-Class and can be Higher-Order:
First-class variables can be passed to functions, can returned from functions and can be stored in data structures.
Higher-Order Functions will take other functions as there input and can also return functions as result.
example:
show_output(f)
f();
print_gfg()
print("hello gfg");
show_output(printgfg)
Variables are immutable.
Advantage of Functional Programming:
- Pure functions are easy to understand, there functional signature give all details. like return type and arguments.
- Make code readable and easily understandable as it will take functions as input as it will act as value.
- Testing and Debugging is easy.
- Help in implementing parallelism and concurrency.
- It adopts lazy evaluation. It save time of making calculation even when not needed.
Disadvantages of Functional Programming:
- Sometimes Pure functions destroy code readability.
- Recursion is hard to write.
- Writing pure function is easy but every hard to integrate it with application and I/O operations.
- Immutable variables and recursion can lead to performance degradation.
Applications:
- Used for mathematical computations.
- Help in concurrency and parallelism.






Comments
Post a Comment