csc241 object-oriented programming (oop) lecture no. 2
DESCRIPTION
Example – Abstraction Attributes - Name- Employee ID - Student Roll No- Designation - Year of Study- Salary - CGPA- Age Ali is a PhD student and teaches BS studentsTRANSCRIPT
![Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/1.jpg)
CSC241 Object-Oriented
Programming (OOP)
Lecture No. 2
![Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/2.jpg)
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”
![Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/3.jpg)
Example – Abstraction
Attributes- Name - Employee ID- Student Roll No - Designation- Year of Study - Salary- CGPA - Age
Ali is a PhD student and teaches BS students
![Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/4.jpg)
Example – Abstraction
behaviour- Study - DevelopExam- GiveExam - TakeExam- PlaySports - Eat- DeliverLecture - Walk
Ali is a PhD student and teaches BS students
![Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/5.jpg)
Example – Abstraction
Attributes- Name - Employee ID- Student Roll No - Designation- Year of Study - Salary- CGPA - Age
Student’s Perspective
![Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/6.jpg)
Example – Abstraction
behaviour- Study - DevelopExam- GiveExam - TakeExam- PlaySports - Eat- DeliverLecture - Walk
Student’s Perspective
![Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/7.jpg)
Example – Abstraction
Attributes- Name - Employee ID- Student Roll No - Designation- Year of Study - Salary- CGPA - Age
Teacher’s Perspective
![Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/8.jpg)
Example – Abstraction
behaviour- Study - DevelopExam- GiveExam - TakeExam- PlaySports - Eat- DeliverLecture - Walk
Teacher’s Perspective
![Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/9.jpg)
Example – Abstraction
Ordinary PerspectiveA pet animal with Four Legs A Tail Two Ears Sharp Teeth
Surgeon’s PerspectiveA being with A Skeleton Heart Kidney Stomach
A cat can be viewed with different perspectives
![Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/10.jpg)
Example – Abstraction
Driver’s View
Engineer’s View
![Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/11.jpg)
Abstraction – Advantages
Simplifies the model by hiding irrelevant details
Abstraction provides the freedom to defer implementation decisions by avoiding commitment to details
![Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/12.jpg)
Classes
In an OO model, some of the objects exhibit identical characteristics (information structure and behaviour)
We say that they belong to the same class
![Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/13.jpg)
Example – Class
Ali studies mathematics Anam studies physics Sohail studies chemistry
Each one is a Student We say these objects are instances of the Student
class
![Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/14.jpg)
Example – Class
Ahsan teaches mathematics Aamir teaches computer science Atif teaches physics
Each one is a teacher We say these objects are instances of the Teacher
class
![Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/15.jpg)
Graphical Representation of Classes
(Class Name)
(attributes)
(operations)
(Class Name)
Normal Form
Suppressed Form
![Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/16.jpg)
Example – Graphical Representation of Classes
CirclecenterradiusdrawcomputeArea
Normal Form
Suppressed Form
Circle
![Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/17.jpg)
Example – Graphical Representation of ClassesPerson
nameagegendereatwalk
Normal Form
Suppressed Form
Person
![Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/18.jpg)
Inheritance
A child inherits characteristics of its parents
Besides inherited characteristics, a child may have its own unique characteristics
![Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/19.jpg)
Inheritance in Classes
If a class B inherits from class A then it contains all the characteristics (information structure and behaviour) of class A
The parent class is called base class and the child class is called derived class
Besides inherited characteristics, derived class may have its own unique characteristics
![Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/20.jpg)
Example – Inheritance
Person
TeacherDoctorStudent
![Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/21.jpg)
Example – Inheritance
Shape
CircleTriangleLine
![Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/22.jpg)
Inheritance – “IS A” or“IS A KIND OF” Relationship Each derived class is a special kind of its base
class
![Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/23.jpg)
Example – “IS A” RelationshipPerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/24.jpg)
Example – “IS A” Relationship
ShapecolorcoorddrawrotatesetColor
CircleradiusdrawcomputeArea
Linelengthdraw
TriangleangledrawcomputeArea
![Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/25.jpg)
Inheritance – Advantages
Reuse
Less redundancy
Increased maintainability
![Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/26.jpg)
Reuse with Inheritance
Main purpose of inheritance is reuse We can easily add new classes by inheriting from
existing classes Select an existing class closer to the desired
functionality Create a new class and inherit it from the selected
class Add to and/or modify the inherited functionality
![Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/27.jpg)
Example ReuseShape
colorcoorddrawrotatesetColor
CircleradiusdrawcomputeArea
Linelengthdraw
TriangleangledrawcomputeArea
![Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/28.jpg)
Example ReusePerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/29.jpg)
Example ReusePerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/30.jpg)
Recap – Inheritance
Derived class inherits all the characteristics of the base class
Besides inherited characteristics, derived class may have its own unique characteristics
Major benefit of inheritance is reuse
![Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/31.jpg)
Concepts Related with Inheritance
Generalization
Subtyping (extension)
Specialization (restriction)
![Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/32.jpg)
Generalization
In OO models, some classes may have common characteristics
We extract these features into a new class and inherit original classes from this new class
This concept is known as Generalization
![Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/33.jpg)
Example – Generalization
CirclecolorverticesradiusmovesetColorcomputeArea
LinecolorverticeslengthmovesetColorgetLength
TrianglecolorverticesanglemovesetColorcomputeArea
![Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/34.jpg)
Example – GeneralizationShape
colorverticesmovesetColor
CircleradiuscomputeArea
LinelengthgetLength
TriangleanglecomputeArea
![Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/35.jpg)
Example – Generalization
TeachernameagegenderdesignationsalaryteachtakeExameatwalk
StudentnameagegenderprogramstudyYearstudyheldExameatwalk
DoctornameagegenderdesignationsalarycheckUpprescribeeatwalk
![Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/36.jpg)
Example – GeneralizationPerson
nameagegendereatwalk
TeacherdesignationsalaryteachtakeExam
StudentprogramstudyYearstudyheldExam
DoctordesignationsalarycheckUpprescribe
![Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/37.jpg)
Sub-typing & Specialization We want to add a new class to an existing model
Find an existing class that already implements some of the desired state and behaviour
Inherit the new class from this class and add unique behaviour to the new class
![Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/38.jpg)
Sub-typing (Extension)
Sub-typing means that derived class is behaviourally compatible with the base class
Behaviourally compatible means that base class can be replaced by the derived class
![Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/39.jpg)
Example –Sub-typing (Extension)
Personnameagegendereatswalks
StudentprogramstudyYearstudytakeExam
![Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/40.jpg)
Example – Sub-typing (Extension)
ShapecolorverticessetColormove
CircleradiuscomputeCFcomputeArea
![Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/41.jpg)
Specialization (Restriction)
Specialization means that derived class is behaviourally incompatible with the base class
Behaviourally incompatible means that base class can’t always be replaced by the derived class
![Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/42.jpg)
Example – Specialization (Restriction)
Personage : [0..100]…
Adultage : [18..100]…
setAge( a )…
setAge( a )…
age = a
If age < 18 then errorelse age = a
![Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/43.jpg)
Example – Specialization (Restriction)
IntegerSet…
NaturalSet…
add( elem )…
add( elem )…
add element to the set
If elem < 1 then errorelse
add element to the set
![Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/44.jpg)
Overriding
A class may need to override the default behaviour provided by its base class
Reasons for overriding Provide behaviour specific to a derived class Extend the default behaviour Restrict the default behaviour Improve performance
![Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/45.jpg)
Example – Specific Behaviour Shape
colorverticesdrawmovesetColor
CircleradiusdrawcomputeArea
Linelengthdraw
TriangleangledrawcomputeArea
![Page 46: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/46.jpg)
Example – ExtensionWindowwidthheightopenclosedraw
DialogBoxcontrolsenabledraw
1- Invoke Window’s draw2- draw the dialog box
![Page 47: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/47.jpg)
Example – RestrictionIntegerSet
…
NaturalSet…
add( elem )…
add( elem )…
Add element to the set
If elem < 1 then give errorelse
Add element to the set
![Page 48: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/48.jpg)
Example – Improve Performance
Class Circle overrides rotate operation of class Shape with a Null operation.
ShapecolorcoorddrawrotatesetColor
Circleradiusdrawrotate
![Page 49: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/49.jpg)
Abstract Classes
An abstract class implements an abstract concept Main purpose is to be inherited by other classes Can’t be instantiated Promotes reuse
![Page 50: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/50.jpg)
Example – Abstract Classes
TeacherDoctorStudent
Here, Person is an abstract class
Personnameagegendereatwalk
![Page 51: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/51.jpg)
Example – Abstract Classes
BusTruckCar
Here, Vehicle is an abstract class
VehiclecolormodelaccelerateapplyBrakes
![Page 52: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/52.jpg)
Concrete Classes
A concrete class implements a concrete concept
Main purpose is to be instantiated
Provides implementation details specific to the domain context
![Page 53: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/53.jpg)
Example – Concrete Classes
Here, Student, Teacher and Doctor are concrete classes
TeacherDoctorStudent
programstudyYearstudyheldExam
Person
![Page 54: CSC241 Object-Oriented Programming (OOP) Lecture No. 2](https://reader035.vdocuments.net/reader035/viewer/2022081421/5a4d1bf17f8b9ab0599e5fdb/html5/thumbnails/54.jpg)
Example – Concrete Classes
• Here, Car, Bus and Truck are concrete classes
BusCar
Vehicle
Truckcapacityloadunload