OOP CLASS + INHERITANCE
-
Upload
university-of-central-punjab -
Category
Education
-
view
458 -
download
3
description
Transcript of OOP CLASS + INHERITANCE
![Page 1: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/1.jpg)
Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)
Lecture No. 3Lecture No. 3
![Page 2: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/2.jpg)
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”
![Page 3: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/3.jpg)
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
Ali is a PhD student and teaches BS students
![Page 4: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/4.jpg)
Example – AbstractionExample – Abstraction
Ali 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
![Page 5: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/5.jpg)
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
![Page 6: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/6.jpg)
Example – AbstractionExample – Abstraction
Student’s Perspective
►behaviourbehaviour- Study- Study - DevelopExam- DevelopExam
- GiveExam- GiveExam - TakeExam- TakeExam
- PlaySports- PlaySports - Eat- Eat
- DeliverLecture- DeliverLecture - Walk- Walk
![Page 7: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/7.jpg)
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
![Page 8: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/8.jpg)
Example – AbstractionExample – Abstraction
Teacher’s Perspective
►behaviourbehaviour- Study- Study - DevelopExam- DevelopExam
- GiveExam- GiveExam - TakeExam- TakeExam
- PlaySports- PlaySports - Eat- Eat
- DeliverLecture- DeliverLecture - Walk- Walk
![Page 9: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/9.jpg)
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
![Page 10: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/10.jpg)
Example – AbstractionExample – Abstraction
Driver’s View
Engineer’s View
![Page 11: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/11.jpg)
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
![Page 12: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/12.jpg)
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
![Page 13: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/13.jpg)
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
![Page 14: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/14.jpg)
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
![Page 15: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/15.jpg)
Graphical Representation of Graphical Representation of ClassesClasses
(Class Name)
(attributes)
(operations)
(Class Name)
Normal Form
Suppressed Form
![Page 16: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/16.jpg)
Example – Graphical Example – Graphical Representation of ClassesRepresentation of Classes
Circlecenterradius
drawcomputeArea
Normal Form
Suppressed Form
Circle
![Page 17: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/17.jpg)
Example – Graphical Example – Graphical Representation of ClassesRepresentation of Classes
Personnameagegendereatwalk
Normal Form
Suppressed Form
Person
![Page 18: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/18.jpg)
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
![Page 19: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/19.jpg)
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
![Page 20: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/20.jpg)
Example – InheritanceExample – Inheritance
Person
TeacherDoctorStudent
![Page 21: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/21.jpg)
Example – InheritanceExample – Inheritance
Shape
CircleTriangleLine
![Page 22: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/22.jpg)
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
![Page 23: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/23.jpg)
Example – “IS A” RelationshipExample – “IS A” RelationshipPerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 24: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/24.jpg)
Example – “IS A” RelationshipExample – “IS A” Relationship
ShapecolorcoorddrawrotatesetColor
CircleradiusdrawcomputeArea
Linelengthdraw
Triangleangle
drawcomputeArea
![Page 25: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/25.jpg)
Inheritance – AdvantagesInheritance – Advantages
►ReuseReuse
►Less redundancyLess redundancy
► Increased maintainabilityIncreased maintainability
![Page 26: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/26.jpg)
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
functionalityfunctionality
![Page 27: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/27.jpg)
Example ReuseExample ReuseShape
colorcoorddrawrotatesetColor
CircleradiusdrawcomputeArea
Linelengthdraw
Triangleangle
drawcomputeArea
![Page 28: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/28.jpg)
Example ReuseExample ReusePerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 29: OOP CLASS + INHERITANCE](https://reader035.fdocuments.us/reader035/viewer/2022062304/55901d791a28ab3b508b477a/html5/thumbnails/29.jpg)
Example ReuseExample ReusePerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe