29
Object-Oriented Object-Oriented Programming (OOP) Programming (OOP) Lecture No. 4 Lecture No. 4 Downloaded From: www.bsit.zxq.net

Object-Oriented Programming (OOP) Lecture No. 4

  • Upload
    piera

  • View
    41

  • Download
    4

Embed Size (px)

DESCRIPTION

Object-Oriented Programming (OOP) Lecture No. 4. Abstraction. Abstraction is a way to cope with complexity. Principle of abstraction: “Capture only those details about an object that are relevant to current perspective”. Attributes - Name- Employee ID - Student Roll No- Designation - PowerPoint PPT Presentation

Citation preview

Page 1: Object-Oriented Programming (OOP) Lecture No.  4

Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)

Lecture No. 4Lecture No. 4

Downloaded From: www.bsit.zxq.net

Page 2: Object-Oriented Programming (OOP) Lecture No.  4

AbstractionAbstraction

►Abstraction is a way to cope with Abstraction is a way to cope with complexity.complexity.

►Principle of abstraction:Principle of abstraction:

““Capture only those details about an object Capture only those details about an object that are relevant to current perspective”that are relevant to current perspective”

Downloaded From: www.bsit.zxq.net

Page 3: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

►AttributesAttributes- Name- Name - Employee - Employee

IDID

- Student Roll No- Student Roll No - Designation- Designation

- Year of Study- Year of Study - Salary- Salary

- CGPA- CGPA - Age- Age

Nabeel is a PhD student and teaches BS students

Downloaded From: www.bsit.zxq.net

Page 4: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

Nabeel is a PhD student and teaches BS students

►behaviourbehaviour- Study- Study - DevelopExam- DevelopExam

- GiveExam- GiveExam - TakeExam- TakeExam

- PlaySports- PlaySports - Eat- Eat

- DeliverLecture- DeliverLecture - Walk- Walk

Downloaded From: www.bsit.zxq.net

Page 5: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

►AttributesAttributes- Name- Name - Employee - Employee

IDID

- Student Roll No- Student Roll No - Designation- Designation

- Year of Study- Year of Study - Salary- Salary

- CGPA- CGPA - Age- Age

Student’s Perspective

Downloaded From: www.bsit.zxq.net

Page 6: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

Student’s Perspective

►behaviourbehaviour- Study- Study - DevelopExam- DevelopExam

- GiveExam- GiveExam - TakeExam- TakeExam

- PlaySports- PlaySports - Eat- Eat

- DeliverLecture- DeliverLecture - Walk- Walk

Downloaded From: www.bsit.zxq.net

Page 7: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

►AttributesAttributes- Name- Name - Employee - Employee

IDID

- Student Roll No- Student Roll No - Designation- Designation

- Year of Study- Year of Study - Salary- Salary

- CGPA- CGPA - Age- Age

Teacher’s Perspective

Downloaded From: www.bsit.zxq.net

Page 8: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

Teacher’s Perspective

►behaviourbehaviour- Study- Study - DevelopExam- DevelopExam

- GiveExam- GiveExam - TakeExam- TakeExam

- PlaySports- PlaySports - Eat- Eat

- DeliverLecture- DeliverLecture - Walk- Walk

Downloaded From: www.bsit.zxq.net

Page 9: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

►Ordinary PerspectiveOrdinary PerspectiveA pet animal withA pet animal with Four LegsFour Legs A TailA Tail Two EarsTwo Ears Sharp TeethSharp Teeth

►Surgeon’s PerspectiveSurgeon’s PerspectiveA being withA being with A SkeletonA Skeleton HeartHeart KidneyKidney StomachStomach

A cat can be viewed with different perspectives

Downloaded From: www.bsit.zxq.net

Page 10: Object-Oriented Programming (OOP) Lecture No.  4

Example – AbstractionExample – Abstraction

Driver’s View

Engineer’s View

Downloaded From: www.bsit.zxq.net

Page 11: Object-Oriented Programming (OOP) Lecture No.  4

Abstraction – AdvantagesAbstraction – Advantages

►Simplifies the model by hiding Simplifies the model by hiding irrelevant detailsirrelevant details

►Abstraction provides the freedom to Abstraction provides the freedom to defer implementation decisions by defer implementation decisions by avoiding commitment to detailsavoiding commitment to details

Downloaded From: www.bsit.zxq.net

Page 12: Object-Oriented Programming (OOP) Lecture No.  4

ClassesClasses

► In an OO model, some of the objects In an OO model, some of the objects exhibit identical characteristics exhibit identical characteristics (information structure and behaviour)(information structure and behaviour)

►We say that they belong to the same We say that they belong to the same classclass

Downloaded From: www.bsit.zxq.net

Page 13: Object-Oriented Programming (OOP) Lecture No.  4

Example – ClassExample – Class

►Ali studies mathematicsAli studies mathematics►Anam studies physicsAnam studies physics►Sohail studies chemistrySohail studies chemistry

►Each one is a StudentEach one is a Student►We say these objects are We say these objects are instancesinstances of of

the Student classthe Student class

Downloaded From: www.bsit.zxq.net

Page 14: Object-Oriented Programming (OOP) Lecture No.  4

Example – ClassExample – Class

►Ahsan teaches mathematicsAhsan teaches mathematics►Aamir teaches computer scienceAamir teaches computer science►Atif teaches physicsAtif teaches physics

►Each one is a teacherEach one is a teacher►We say these objects are We say these objects are instancesinstances of of

the Teacher classthe Teacher class

Downloaded From: www.bsit.zxq.net

Page 15: Object-Oriented Programming (OOP) Lecture No.  4

Graphical Representation of Graphical Representation of ClassesClasses

(Class Name)

(attributes)

(operations)

(Class Name)

Normal Form

Suppressed Form

Downloaded From: www.bsit.zxq.net

Page 16: Object-Oriented Programming (OOP) Lecture No.  4

Example – Graphical Example – Graphical Representation of ClassesRepresentation of Classes

Circlecenterradius

drawcomputeArea

Normal Form

Suppressed Form

Circle

Downloaded From: www.bsit.zxq.net

Page 17: Object-Oriented Programming (OOP) Lecture No.  4

Example – Graphical Example – Graphical Representation of ClassesRepresentation of Classes

Personnameagegendereatwalk

Normal Form

Suppressed Form

Person

Downloaded From: www.bsit.zxq.net

Page 18: Object-Oriented Programming (OOP) Lecture No.  4

InheritanceInheritance

►A child inherits characteristics of its A child inherits characteristics of its parentsparents

►Besides inherited characteristics, a Besides inherited characteristics, a child may have its own unique child may have its own unique characteristicscharacteristics

Downloaded From: www.bsit.zxq.net

Page 19: Object-Oriented Programming (OOP) Lecture No.  4

Inheritance in ClassesInheritance in Classes

► If a class B inherits from class A then it If a class B inherits from class A then it contains all the characteristics contains all the characteristics (information structure and behaviour) of (information structure and behaviour) of class Aclass A

►The parent class is called The parent class is called basebase class and class and the child class is called the child class is called derivedderived class class

►Besides inherited characteristics, Besides inherited characteristics, derived class may have its own unique derived class may have its own unique characteristicscharacteristics

Downloaded From: www.bsit.zxq.net

Page 20: Object-Oriented Programming (OOP) Lecture No.  4

Example – InheritanceExample – Inheritance

Person

TeacherDoctorStudent

Downloaded From: www.bsit.zxq.net

Page 21: Object-Oriented Programming (OOP) Lecture No.  4

Example – InheritanceExample – Inheritance

Shape

CircleTriangleLine

Downloaded From: www.bsit.zxq.net

Page 22: Object-Oriented Programming (OOP) Lecture No.  4

Inheritance – “IS A” orInheritance – “IS A” or“IS A KIND OF” Relationship“IS A KIND OF” Relationship

►Each derived class is a special kind of Each derived class is a special kind of its base classits base class

Downloaded From: www.bsit.zxq.net

Page 23: Object-Oriented Programming (OOP) Lecture No.  4

Example – “IS A” RelationshipExample – “IS A” RelationshipPerson

nameagegendereatwalk

TeacherdesignationsalaryteachtakeExam

StudentprogramstudyYearstudyheldExam

DoctordesignationsalarycheckUpprescribeDownloaded From: www.bsit.zxq.net

Page 24: Object-Oriented Programming (OOP) Lecture No.  4

Example – “IS A” RelationshipExample – “IS A” Relationship

ShapecolorcoorddrawrotatesetColor

CircleradiusdrawcomputeArea

Linelengthdraw

Triangleangle

drawcomputeAreaDownloaded From: www.bsit.zxq.net

Page 25: Object-Oriented Programming (OOP) Lecture No.  4

Inheritance – AdvantagesInheritance – Advantages

►ReuseReuse

►Less redundancyLess redundancy

► Increased maintainabilityIncreased maintainability

Downloaded From: www.bsit.zxq.net

Page 26: Object-Oriented Programming (OOP) Lecture No.  4

Reuse with InheritanceReuse with Inheritance

►Main purpose of inheritance is reuseMain purpose of inheritance is reuse►We can easily add new classes by We can easily add new classes by

inheriting from existing classesinheriting from existing classes Select an existing class closer to the Select an existing class closer to the

desired functionalitydesired functionality Create a new class and inherit it from the Create a new class and inherit it from the

selected classselected class Add to and/or modify the inherited Add to and/or modify the inherited

functionalityfunctionalityDownloaded From: www.bsit.zxq.net

Page 27: Object-Oriented Programming (OOP) Lecture No.  4

Example ReuseExample ReuseShape

colorcoorddrawrotatesetColor

CircleradiusdrawcomputeArea

Linelengthdraw

Triangleangle

drawcomputeAreaDownloaded From: www.bsit.zxq.net

Page 28: Object-Oriented Programming (OOP) Lecture No.  4

Example ReuseExample ReusePerson

nameagegendereatwalk

TeacherdesignationsalaryteachtakeExam

StudentprogramstudyYearstudyheldExam

DoctordesignationsalarycheckUpprescribeDownloaded From: www.bsit.zxq.net

Page 29: Object-Oriented Programming (OOP) Lecture No.  4

Example ReuseExample ReusePerson

nameagegendereatwalk

TeacherdesignationsalaryteachtakeExam

StudentprogramstudyYearstudyheldExam

DoctordesignationsalarycheckUpprescribeDownloaded From: www.bsit.zxq.net