Object Oriented Programming (JAVA)
— -Short text — -
· What is the Object Oriented Programming ?
Simply, Object oriented programming is a programming paradigm based on the concept of “objects”. Object can contain data (in the form of field) and code.
Also OOP is a software design around data or objects also functions and logic.
Before the OOP we had procedural programming. That devided program into a set of functions. So we have data store in bunch of variables and functions operate under data.
f() — function.
x — variable .
Mostly programming languages grow bunch of functions of the end. Like that under diagram. All the functions are coping and pasting over and over.
Then one function was changed .we have to change several functions. Unless functions break. Object oriented programming can solve these problems.
Object oriented programming with combine grouped if related variables and functions into a unit. We called that unit an “object”. We refer to these variables as properties and functions as methods.
Think about this example — car is an object with properties like make,model ,color and methods like start(), stop() , move().
OOP has four concept,
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
- Encapsulation.
OOP, We group related variables and functions that operate under them into an object. We called that Encapsulation.
This is an example of that action.
Advantages of Encapsulation :
1. It improves maintainability and flexibility and re-usability.
Eg:- above code void setEmName(String name) and void getEmpName() can be changed any point of time.
2. The field can be made read-only or write-only.
Encapsulation is also known as “data-hiding”.
2.Abstraction
Simply, think about DVD player as an object. DVD player has a complex logic board inside and buttons outside the DVD player. You simply press the button you don’t care what the happened on the inside. It is complex task. It’s hidden from you. This is the abstraction technique.
Then we can same technique an object. So we can hide some properties and methods from the outside. This gives couple of benefits.
1. Simpler interface — Using and understanding an object with the few properties and methods.
2. Reduce the impact of change — Imagine some day of future you have to change the inner private property or method , not of the changes leak to the out side.
This is an example of that action.
3. Inheritance
Inheritance is a mechanism that allows you to illuminate redundant code. Simply , think about html element like textboxes,checkboxes…. All these element have few things in the common . It has properties like hidden , innerHTML and methods like click(),focus() instead of pre-define all these properties and methods for every type of html element. We can define the one in generic object called HTML Element and other object inherit these properties and methods. So inheritance helps us illuminate redundant code.
This is an example of that action.
4.Polymorphism
Poly means many. Morphism means form . Then polymorphism means many forms. OOP polymorphism is a technique that allows you to get with long if and else or switch and case statement.
This is an example of that action.
Here this is the benefits of OOP.
1.Encapsulation :- reduce complexity + increase reusability.
2.Abstraction :- reduce complexity + isolate impact of changes.
3.Inheritance :- illuminate redundant code.
4.Polymorphism :- refactor ugly switch/case statement.