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

Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Embed Size (px)

Citation preview

Page 1: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

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

Lecture No. 4Lecture No. 4

Page 2: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Recap – InheritanceRecap – Inheritance

►Derived class inherits all the Derived class inherits all the characteristics of the base classcharacteristics of the base class

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

►Major benefit of inheritance is reuseMajor benefit of inheritance is reuse

Page 3: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Concepts Related with Concepts Related with InheritanceInheritance

►GeneralizationGeneralization

►Subtyping (extension)Subtyping (extension)

►Specialization (restriction)Specialization (restriction)

Page 4: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

GeneralizationGeneralization

► In OO models, some classes may have In OO models, some classes may have common characteristicscommon characteristics

►We extract these features into a new We extract these features into a new class and inherit original classes from class and inherit original classes from this new classthis new class

►This concept is known as GeneralizationThis concept is known as Generalization

Page 5: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – GeneralizationExample – Generalization

CirclecolorverticesradiusmovesetColorcomputeArea

LinecolorverticeslengthmovesetColorgetLength

Trianglecolorverticesangle

movesetColorcomputeArea

Page 6: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – GeneralizationExample – Generalization

ShapecolorverticesmovesetColor

CircleradiuscomputeArea

LinelengthgetLength

Triangleangle

computeArea

Page 7: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – GeneralizationExample – Generalization

TeachernameagegenderdesignationsalaryteachtakeExameatwalk

StudentnameagegenderprogramstudyYearstudyheldExameatwalk

DoctornameagegenderdesignationsalarycheckUpprescribeeatwalk

Page 8: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – GeneralizationExample – GeneralizationPerson

nameagegendereatwalk

TeacherdesignationsalaryteachtakeExam

StudentprogramstudyYearstudyheldExam

DoctordesignationsalarycheckUpprescribe

Page 9: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Sub-typing & SpecializationSub-typing & Specialization

►We want to add a new class to an We want to add a new class to an existing modelexisting model

►Find an existing class that already Find an existing class that already implements some of the desired state implements some of the desired state and behaviourand behaviour

► Inherit the new class from this class and Inherit the new class from this class and add unique behaviour to the new classadd unique behaviour to the new class

Page 10: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Sub-typing (Extension)Sub-typing (Extension)

►Sub-typing means that derived class is Sub-typing means that derived class is behaviourally compatible with the behaviourally compatible with the base classbase class

►Behaviourally compatible means that Behaviourally compatible means that base class can be replaced by the base class can be replaced by the derived classderived class

Page 11: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example –Example –Sub-typing Sub-typing (Extension)(Extension)

Personnameagegendereatswalks

StudentprogramstudyYear

studytakeExam

Page 12: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Example – Sub-typing Sub-typing (Extension)(Extension)

ShapecolorverticessetColormove

CircleradiuscomputeCFcomputeArea

Page 13: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Specialization (Restriction)Specialization (Restriction)

►Specialization means that derived Specialization means that derived class is behaviourally incompatible class is behaviourally incompatible with the base classwith the base class

►Behaviourally incompatible means that Behaviourally incompatible means that base class can’t always be replaced by base class can’t always be replaced by the derived classthe derived class

Page 14: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Specialization Example – Specialization (Restriction)(Restriction)

Personage : [0..100]…

Adultage : [18..100]…

setAge( a )…

setAge( a )…

age = a

If age < 18 then errorelse age = a

Page 15: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Specialization Example – Specialization (Restriction)(Restriction)

IntegerSet…

NaturalSet…

add( elem )…

add( elem )…

add element to the set

If elem < 1 then errorelse

add element to the set

Page 16: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

OverridingOverriding

►A class may need to override the default A class may need to override the default behaviour provided by its base classbehaviour provided by its base class

►Reasons for overridingReasons for overriding Provide behaviour specific to a derived classProvide behaviour specific to a derived class Extend the default behaviourExtend the default behaviour Restrict the default behaviourRestrict the default behaviour Improve performanceImprove performance

Page 17: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Specific BehaviourExample – Specific BehaviourShape

colorverticesdrawmovesetColor

CircleradiusdrawcomputeArea

Linelengthdraw

Triangleangle

drawcomputeArea

Page 18: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – ExtensionExample – Extension

Windowwidthheightopenclosedraw

DialogBoxcontrolsenabledraw

1- Invoke Window’s draw2- draw the dialog box

Page 19: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – RestrictionExample – Restriction

IntegerSet…

NaturalSet…

add( elem )…

add( elem )…

Add element to the set

If elem < 1 then give errorelse

Add element to the set

Page 20: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Improve PerformanceExample – Improve Performance

► Class Circle Class Circle overrides overrides rotaterotate operation of class operation of class Shape with a Null Shape with a Null operation.operation.

ShapecolorcoorddrawrotatesetColor

Circleradiusdrawrotate

Page 21: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Abstract ClassesAbstract Classes

►An abstract class implements an An abstract class implements an abstract conceptabstract concept

►Main purpose is to be inherited by Main purpose is to be inherited by other classesother classes

►Can’t be instantiatedCan’t be instantiated►Promotes reusePromotes reuse

Page 22: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Abstract ClassesExample – Abstract Classes

TeacherDoctorStudent

►Here, Person is an abstract classHere, Person is an abstract class

Personnameagegendereatwalk

Page 23: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Abstract ClassesExample – Abstract Classes

BusTruckCar

►Here, Vehicle is an abstract classHere, Vehicle is an abstract class

VehiclecolormodelaccelerateapplyBrakes

Page 24: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Concrete ClassesConcrete Classes

►A concrete class implements a A concrete class implements a concrete conceptconcrete concept

►Main purpose is to be instantiatedMain purpose is to be instantiated

►Provides implementation details Provides implementation details specific to the domain contextspecific to the domain context

Page 25: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Concrete ClassesExample – Concrete Classes

►Here, Student, Teacher and Doctor Here, Student, Teacher and Doctor are concrete classesare concrete classes

TeacherDoctorStudent

programstudyYearstudyheldExam

Person

Page 26: Object-Oriented Programming (OOP) Lecture No. 4. Recap – Inheritance ► Derived class inherits all the characteristics of the base class ► Besides inherited

Example – Concrete ClassesExample – Concrete Classes

• Here, Car, Bus and Truck are concrete classes

BusCar

Vehicle

Truckcapacityloadunload