8/3/2019 Computer Notes - Abstraction
1/29
AbstractionAbstraction
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
2/29
AbstractionAbstraction
Abstraction is a way to cope with complexity.Abstraction is a way to cope with complexity.
Principle of abstraction:Principle of abstraction:
Capture only those details about an object thatCapture only those details about an object thatare relevant to current perspectiveare relevant to current perspective
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
3/29
ExampleExampleAbstractionAbstraction
AttributesAttributes
-- NameName -- Employee IDEmployee ID
-- Student Roll NoStudent Roll No -- DesignationDesignation
--Year of StudyYear of Study -- SalarySalary
-- CGPACGPA --AgeAge
Ali is a PhD student and teaches BS students
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
4/29
ExampleExampleAbstractionAbstractionAli is a PhD student and teaches BS students
behaviourbehaviour
-- StudyStudy -- DevelopExamDevelopExam
-- GiveExamGiveExam -- TakeExamTakeExam
-- PlaySportsPlaySports -- EatEat
-- DeliverLectureDeliverLecture -- WalkWalk
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
5/29
ExampleExampleAbstractionAbstraction
AttributesAttributes
-- NameName -- Employee IDEmployee ID
-- Student Roll NoStudent Roll No -- DesignationDesignation
--Year of StudyYear of Study -- SalarySalary
-- CGPACGPA --AgeAge
Students Perspective
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
6/29
ExampleExampleAbstractionAbstractionStudents Perspective
behaviourbehaviour
-- StudyStudy -- DevelopExamDevelopExam
-- GiveExamGiveExam -- TakeExamTakeExam
-- PlaySportsPlaySports -- EatEat
-- DeliverLectureDeliverLecture -- WalkWalk
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
7/29
ExampleExampleAbstractionAbstraction
AttributesAttributes
-- NameName -- Employee IDEmployee ID
-- Student Roll NoStudent Roll No -- DesignationDesignation
--Year of StudyYear of Study -- SalarySalary
-- CGPACGPA --AgeAge
Teachers Perspective
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
8/29
ExampleExampleAbstractionAbstractionTeachers Perspective
behaviourbehaviour
-- StudyStudy -- DevelopExamDevelopExam
-- GiveExamGiveExam -- TakeExamTakeExam
-- PlaySportsPlaySports -- EatEat
-- DeliverLectureDeliverLecture -- WalkWalk
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
9/29
ExampleExampleAbstractionAbstraction
Ordinary PerspectiveOrdinary Perspective
A pet animal withA pet animal with
Four LegsFour Legs
A TailA Tail Two EarsTwo Ears
Sharp TeethSharp Teeth
SurgeonSurgeons Perspectives Perspective
A being withA being with
A SkeletonA Skeleton
HeartHeart KidneyKidney
StomachStomach
A cat can be viewed with different perspectives
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
10/29
ExampleExampleAbstractionAbstraction
Drivers View
Engineers View
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
11/29
AbstractionAbstractionAdvantagesAdvantages
Simplifies the model by hiding irrelevant detailsSimplifies the model by hiding irrelevant details
Abstraction provides the freedom to deferAbstraction provides the freedom to deferimplementation decisions by avoidingimplementation decisions by avoiding
commitment to detailscommitment to details
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
12/29
ClassesClasses
In an OO model, some of the objects exhibitIn an OO model, some of the objects exhibit
identical characteristics (information structureidentical characteristics (information structure
andand behaviourbehaviour))
We say that they belong to the same classWe say that they belong to the same class
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
13/29
ExampleExample ClassClass
Ali studies mathematicsAli studies mathematics
AnamAnam studies physicsstudies physics
SohailSohail studies chemistrystudies chemistry
Each one is a StudentEach one is a Student
We say these objects areWe say these objects areinstancesinstances
of theof theStudent classStudent class
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
14/29
ExampleExample ClassClass
AhsanAhsan teaches mathematicsteaches mathematics
AamirAamir teaches computer scienceteaches computer science
AtifAtifteaches physicsteaches physics
Each one is a teacherEach one is a teacher
We say these objects areWe say these objects areinstancesinstances
of theof theTeacher classTeacher class
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
15/29
Graphical Representation of ClassesGraphical Representation of Classes
(Class Name)
(attributes)
(operations)
(Class Name)
Normal Form
Suppressed
Form
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
16/29
ExampleExample Graphical Representation ofGraphical Representation of
ClassesClasses
Circle
center
radiusdraw
computeArea
Normal Form
Suppressed
Form
Circle
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
17/29
ExampleExample Graphical Representation ofGraphical Representation of
ClassesClasses
Personname
age
gendereat
walk
Normal Form
SuppressedForm
Person
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
18/29
InheritanceInheritance
A child inherits characteristics of its parentsA child inherits characteristics of its parents
Besides inherited characteristics, a child mayBesides inherited characteristics, a child mayhave its own unique characteristicshave its own unique characteristics
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
19/29
Inheritance in ClassesInheritance in ClassesIf a class B inherits from class A then it containsIf a class B inherits from class A then it contains
all the characteristics (information structure andall the characteristics (information structure andbehaviourbehaviour) of class A) of class A
The parent class is calledThe parent class is called basebase
class and theclass and the
child class is calledchild class is called derivedderivedclassclass
Besides inherited characteristics, derived classBesides inherited characteristics, derived class
may have its own unique characteristicsmay have its own unique characteristics
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
20/29
ExampleExample InheritanceInheritance
Person
TeacherDoctorStudent
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
21/29
ExampleExample InheritanceInheritance
Shape
CircleTriangleLine
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
22/29
InheritanceInheritanceIS AIS Aoror
IS A KIND OFIS A KIND OFRelationshipRelationship
Each derived class is a special kind of its baseEach derived class is a special kind of its baseclassclass
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
23/29
ExampleExample
IS AIS A
RelationshipRelationship
Personname
agegendereat
walk
Teacherdesignationsalary
teach
takeExam
StudentprogramstudyYear
study
heldExam
Doctordesignationsalary
checkUp
prescribe
8/3/2019 Computer Notes - Abstraction
24/29
ExampleExampleIS AIS ARelationshipRelationship
Shapecolor
coorddraw
rotate
setColor
Circleradius
draw
computeArea
Linelength
draw
Triangleangle
draw
computeArea
8/3/2019 Computer Notes - Abstraction
25/29
InheritanceInheritanceAdvantagesAdvantages
ReuseReuse
Less redundancyLess redundancy
Increased maintainabilityIncreased maintainability
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
26/29
Reuse with InheritanceReuse with InheritanceMain purpose of inheritance is reuseMain purpose of inheritance is reuse
We can easily add new classes by inheritingWe can easily add new classes by inheritingfrom existing classesfrom existing classes
Select an existing class closer to the desiredSelect an existing class closer to the desired
functionalityfunctionality
Create a new class and inherit it from the selectedCreate a new class and inherit it from the selected
classclass
Add to and/or modify the inherited functionalityAdd to and/or modify the inherited functionality
http://ecomputernotes.com
8/3/2019 Computer Notes - Abstraction
27/29
Example ReuseExample ReuseShape
color
coorddraw
rotate
setColor
Circleradius
draw
computeArea
Linelength
draw
Triangleangle
draw
computeArea
8/3/2019 Computer Notes - Abstraction
28/29
Example ReuseExample Reuse
Personname
agegendereat
walk
Teacher
designationsalary
teach
takeExam
Student
programstudyYear
study
heldExam
Doctor
designationsalary
checkUp
prescribe
8/3/2019 Computer Notes - Abstraction
29/29
Example ReuseExample Reuse
Personname
agegendereat
walk
Teacher
designationsalary
teach
takeExam
Student
programstudyYear
study
heldExam
Doctor
designationsalary
checkUp
prescribe