

Hello All,
Today we will spend sometime understanding buzzwords in Object Model or in general Object Oriented Programming Paradigm.
We will talk about:
Abstraction,Encapsulation,Inheritance,Polymorphism &Modularity.
These terms are very frequently used by many people and really these are buzzwords in OOP. few people call these as Major Pillars in OOPs. obviously there are Minor Pillars as well(More on this later).We will define these terms one by one & individually we will understand how simple they are in reality. At the end for sure we will understand all the terms with a simple example. Rather we will consider one object and we will apply all these Buzzwords to that object.
Let's go step by step
1) Abstraction
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
e.g. If we consider Person as a object , then how you will represent Person ?You will write a class Person right? In this class you will go on adding attributes (to represent State of an object) and behaviors (methods) right?
Simple question is which attributes you will add in this class?
answer is "DEPENDS".
Depends on what?
Depends on for what purpose (In which application to be specific) you are going to use this person object. I do agree that few attributes will be common across the applications like firstname, lastname, age, gender etc..
So if you are going to design a object Person for different applications like say health care, Employee Management System, Social Survey related software etc... there could be few attributes which may not appear everywhere.
e.g. 'religion' will not appear at all in a HealthCare System where as it may appear in Social Survey related system. similarly, 'Salary' attribute will be definitely present in Employee Management System/ HRMS application but bit obvious it will not appear in Healthcare System and 'Weight' will be an attribute in Health care application whereas it will be missing in Employee Management System / HRMS applications & so on.....
The bottom line is selecting proper attribute of an object for a specific application is Abstraction.
2) Encapsulation
Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods. 1st Picture depicts this scenario.
e.g. Considering above example of a Person object, HR of the company wants to know the salary details of a Person named ABC. what HR does is on an application he/she will go & search the salary details of Person name 'ABC'. In a fraction of second all the data is available on the screen. HR as a user never understands which attribute is accessed or where it is stored (in database/ in file etc..) as well as end user has no idea how it is fetched from a source? what kind of processing is done on it ?(if any) & how it was displayed?
OOPs ensures that data is not accessed accidentally/intentionally directly but only through methods (behavior).
If i present a class Calculator in front of you with a method written inside it which accepts an integer & does a job of finding factorial of a number say its name is
int calculateFactorial(int number)
So you are the end user & now you know that passing an integer to this method will result in producing factorial of that specific number right?
As most of us know that there are two ways of finding out factorial of a number:
In a Recursive fashion and
In a Non- Recursive fashion..........right?
Now as a end user you don't have to worry what type of implementation is used inside a method as long as you are getting correct results ....am i correct?
This is one more example of Encapsulation
So , Encapsulation does a separation between interface (methods you see as a class user) & it' s implementation (how exactly these methods are written inside a class).
Finding it easier?
3) Inheritance
Inheritance basically refers to hierarchy to reduce complexity.
In OOPs it's a class hierarchy. You will have some class "A" doing some stuff for you & later on you would realize that you need one more class say Class "B" which will be lot similar to A(not saying identical) and B has something extra in it. so B itself is a A plus something extra(more specialized). so in this case what we will do is we will write a class which will extend class A will put some additional functionality in this new class & we will call that Class as Class B. so we are creating a hierarchy of classes which represents Generalization-Specialization hierrachy
e.g.
Vehicle is parked on a Road doesn't make much sense or doesn't carry much information as opposed to Two Wheeler (little bit specific) or Four Wheeler is parked on a Road does. Honda bike Parked on a Road is more specfic compared to all the above statements.
You can have a hierarhy say Vehicle as a generic class and TwoWheeler, ThreeWheeler, FourWheeler extending from them. again you can have a classification that would go on say for two wheeler as Honda bike and Suzuki bikes & so on same applies for any Vehicle for that matter. In the diagram as you move downwards you will come across more specific classes/entities and vice versa.
In Java Vehicle class will be called as a Superclass (will be called as Base Class in C++) where as TwoWheeler,ThreeWheeler and Fourwheeler will be individyually called as Subclass of Vehicle. Just to add twist , TwoWheeler class is a Superclass of Class Honda or Honda is a Subclass of class TwoWheeler.
Inheritance of this kind is also known as a "is-a" kind of relationship.
In this hierarchy to express the relationship between Superclass & Subclass you have to use "is-a" word in your sentense.
e.g.
Honda is a TwoWheeler
TwoWheeler is a Vehicle
FourWheeler is a Vehicle
etc..
There is something known as "has-a" kind of relationship as well also known as containment. (more on this later)
4) Polymorphism
Poly means many & morph means forms.
In typical technical stuff definition of Polymorphism is:
It is the ability of different objects to respond to the same message differently.
i am sure nothing is understood by the definition if you are totally new to the world of OOPs.
Let's simplify it:
Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition
Do you use mouse while using your PC?
Have you ever used a right click button of a mouse?
Have you right clicked on Files, Folders, Desktop etc..?
Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....
Sure they are different?
Hey, you understood polymorphism....nothing great right?
Still Puzzled ....Let me explain by revisiting the definition that we have started with
Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.
simple isn't it?
Note: Message is equivalent to calling a method on a object.
5) Modularity
You might have guessed from the name itself what modularity means....Still let's have a look
In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintain & easy to extend.
e.g.
Think about a Car which is made up of a small subsystems like Engine , Wheels, Tyre etc..
If tyre gets punctured you can replace it without touching engine right?
So all subsystems are loosely coupled to built a big System known as a car.
Can you do a simple assignment ?
Think of any Object & try to find out all these Pillars in that object.
In the next post i will do the same, We will consider one object & i will show you how Abstraction,Encapsulation,Inheritance, Polymorphism and Modularity are part of it.......
Till then............Bye Bye
Today we will spend sometime understanding buzzwords in Object Model or in general Object Oriented Programming Paradigm.
We will talk about:
Abstraction,Encapsulation,Inheritance,Polymorphism &Modularity.
These terms are very frequently used by many people and really these are buzzwords in OOP. few people call these as Major Pillars in OOPs. obviously there are Minor Pillars as well(More on this later).We will define these terms one by one & individually we will understand how simple they are in reality. At the end for sure we will understand all the terms with a simple example. Rather we will consider one object and we will apply all these Buzzwords to that object.
Let's go step by step
1) Abstraction
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
e.g. If we consider Person as a object , then how you will represent Person ?You will write a class Person right? In this class you will go on adding attributes (to represent State of an object) and behaviors (methods) right?
Simple question is which attributes you will add in this class?
answer is "DEPENDS".
Depends on what?
Depends on for what purpose (In which application to be specific) you are going to use this person object. I do agree that few attributes will be common across the applications like firstname, lastname, age, gender etc..
So if you are going to design a object Person for different applications like say health care, Employee Management System, Social Survey related software etc... there could be few attributes which may not appear everywhere.
e.g. 'religion' will not appear at all in a HealthCare System where as it may appear in Social Survey related system. similarly, 'Salary' attribute will be definitely present in Employee Management System/ HRMS application but bit obvious it will not appear in Healthcare System and 'Weight' will be an attribute in Health care application whereas it will be missing in Employee Management System / HRMS applications & so on.....
The bottom line is selecting proper attribute of an object for a specific application is Abstraction.
2) Encapsulation
Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods. 1st Picture depicts this scenario.
e.g. Considering above example of a Person object, HR of the company wants to know the salary details of a Person named ABC. what HR does is on an application he/she will go & search the salary details of Person name 'ABC'. In a fraction of second all the data is available on the screen. HR as a user never understands which attribute is accessed or where it is stored (in database/ in file etc..) as well as end user has no idea how it is fetched from a source? what kind of processing is done on it ?(if any) & how it was displayed?
OOPs ensures that data is not accessed accidentally/intentionally directly but only through methods (behavior).
If i present a class Calculator in front of you with a method written inside it which accepts an integer & does a job of finding factorial of a number say its name is
int calculateFactorial(int number)
So you are the end user & now you know that passing an integer to this method will result in producing factorial of that specific number right?
As most of us know that there are two ways of finding out factorial of a number:
In a Recursive fashion and
In a Non- Recursive fashion..........right?
Now as a end user you don't have to worry what type of implementation is used inside a method as long as you are getting correct results ....am i correct?
This is one more example of Encapsulation
So , Encapsulation does a separation between interface (methods you see as a class user) & it' s implementation (how exactly these methods are written inside a class).
Finding it easier?
3) Inheritance
Inheritance basically refers to hierarchy to reduce complexity.
In OOPs it's a class hierarchy. You will have some class "A" doing some stuff for you & later on you would realize that you need one more class say Class "B" which will be lot similar to A(not saying identical) and B has something extra in it. so B itself is a A plus something extra(more specialized). so in this case what we will do is we will write a class which will extend class A will put some additional functionality in this new class & we will call that Class as Class B. so we are creating a hierarchy of classes which represents Generalization-Specialization hierrachy
e.g.
Vehicle is parked on a Road doesn't make much sense or doesn't carry much information as opposed to Two Wheeler (little bit specific) or Four Wheeler is parked on a Road does. Honda bike Parked on a Road is more specfic compared to all the above statements.
You can have a hierarhy say Vehicle as a generic class and TwoWheeler, ThreeWheeler, FourWheeler extending from them. again you can have a classification that would go on say for two wheeler as Honda bike and Suzuki bikes & so on same applies for any Vehicle for that matter. In the diagram as you move downwards you will come across more specific classes/entities and vice versa.
In Java Vehicle class will be called as a Superclass (will be called as Base Class in C++) where as TwoWheeler,ThreeWheeler and Fourwheeler will be individyually called as Subclass of Vehicle. Just to add twist , TwoWheeler class is a Superclass of Class Honda or Honda is a Subclass of class TwoWheeler.
Inheritance of this kind is also known as a "is-a" kind of relationship.
In this hierarchy to express the relationship between Superclass & Subclass you have to use "is-a" word in your sentense.
e.g.
Honda is a TwoWheeler
TwoWheeler is a Vehicle
FourWheeler is a Vehicle
etc..
There is something known as "has-a" kind of relationship as well also known as containment. (more on this later)
4) Polymorphism
Poly means many & morph means forms.
In typical technical stuff definition of Polymorphism is:
It is the ability of different objects to respond to the same message differently.
i am sure nothing is understood by the definition if you are totally new to the world of OOPs.
Let's simplify it:
Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition
Do you use mouse while using your PC?
Have you ever used a right click button of a mouse?
Have you right clicked on Files, Folders, Desktop etc..?
Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....
Sure they are different?
Hey, you understood polymorphism....nothing great right?
Still Puzzled ....Let me explain by revisiting the definition that we have started with
Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.
simple isn't it?
Note: Message is equivalent to calling a method on a object.
5) Modularity
You might have guessed from the name itself what modularity means....Still let's have a look
In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintain & easy to extend.
e.g.
Think about a Car which is made up of a small subsystems like Engine , Wheels, Tyre etc..
If tyre gets punctured you can replace it without touching engine right?
So all subsystems are loosely coupled to built a big System known as a car.
Can you do a simple assignment ?
Think of any Object & try to find out all these Pillars in that object.
In the next post i will do the same, We will consider one object & i will show you how Abstraction,Encapsulation,Inheritance, Polymorphism and Modularity are part of it.......
Till then............Bye Bye
No comments:
Post a Comment