October 27, 2013

Abstraction

Real life objects have a lot of attributes and many kind of behaviors but most of the time we are interested in only that part of the objects that is related to the problem we are currently going to solve, for example in implementing a school system we don’t need to take care of the personnel life of a student or a teacher as it will not effect our system in any way so we will see these objects in the perspective of school system and will ignore their other characteristics, this concept is called “Abstraction”. Abstraction is a way to cope with complexity and it is used to simplify things.

Principle of abstraction:

“Capture only those details about an object that are relevant to current perspective”
Abstraction Example:
Suppose we want to implement abstraction for the following statement.
“Ali is a PhD student and teaches BS students”
Here object Ali has two perspectives one is his student perspective and second is his teacher perspective.
We can sum up Ali’s attributes as follows,
Name
Age
Student Roll No
Year of Study
CGPA
Employee ID
Designation
Salary
As you can see out of all these listed attributes some belong to Ali’s student perspective(Roll No, CGPA, Year of study) and some belong to Ali’s teacher perspective(Employee ID, Designation, Salary).
Similarly we can sum up Ali’s behavior as follows,

Study
DevelopExam
GiveExam
TakeExam
PlaySports
Eat
DeliverLecture
Walk
As was the case with attributes of object Ali, its behavior can also be divided in Ali’s student perspective as well as Ali’s teacher perspective.
Student’s Perspective
Attributes:
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Behaviour:
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Teacher’s Perspective
Attributes:
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Behaviour:
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
A cat can be viewed with different perspectives
Ordinary Perspective
A pet animal with
Four Legs
A Tail
Two Ears
Sharp Teeth
Surgeon’s Perspective
A being with
A Skeleton
Heart
Kidney
Stomach
A car can be viewed with different perspectives.
From driver’s point of view, it has an accelerator, a brake, a clutch, a gear box etc.
From engineer’s point of view, it has an engine, a fuel pump, a rotary motor, shocks etc.
clip_image002
Driver’s View
clip_image004
Engineer’s View

Abstraction – Advantages


Abstraction has following major advantages.
  1. It helps us understanding and solving a problem using object oriented approach as it hides extra irrelevant details of objects.
  1. Focusing on single perspective of an object provides us freedom to change implementation for other aspects of for an object later.
Similar to Encapsulation Abstraction is also used for achieving information hiding as we show only relevant details to related objects, and hide other details.


2 comments:

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...