object-oriented programming (oop) lecture no. 4. recap – inheritance ► derived class inherits...

Post on 03-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Lecture No. 4Lecture No. 4

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

Concepts Related with Concepts Related with InheritanceInheritance

►GeneralizationGeneralization

►Subtyping (extension)Subtyping (extension)

►Specialization (restriction)Specialization (restriction)

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

Example – GeneralizationExample – Generalization

CirclecolorverticesradiusmovesetColorcomputeArea

LinecolorverticeslengthmovesetColorgetLength

Trianglecolorverticesangle

movesetColorcomputeArea

Example – GeneralizationExample – Generalization

ShapecolorverticesmovesetColor

CircleradiuscomputeArea

LinelengthgetLength

Triangleangle

computeArea

Example – GeneralizationExample – Generalization

TeachernameagegenderdesignationsalaryteachtakeExameatwalk

StudentnameagegenderprogramstudyYearstudyheldExameatwalk

DoctornameagegenderdesignationsalarycheckUpprescribeeatwalk

Example – GeneralizationExample – GeneralizationPerson

nameagegendereatwalk

TeacherdesignationsalaryteachtakeExam

StudentprogramstudyYearstudyheldExam

DoctordesignationsalarycheckUpprescribe

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

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

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

Personnameagegendereatswalks

StudentprogramstudyYear

studytakeExam

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

ShapecolorverticessetColormove

CircleradiuscomputeCFcomputeArea

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

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

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

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

Example – Specific BehaviourExample – Specific BehaviourShape

colorverticesdrawmovesetColor

CircleradiusdrawcomputeArea

Linelengthdraw

Triangleangle

drawcomputeArea

Example – ExtensionExample – Extension

Windowwidthheightopenclosedraw

DialogBoxcontrolsenabledraw

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

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

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

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

Example – Abstract ClassesExample – Abstract Classes

TeacherDoctorStudent

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

Personnameagegendereatwalk

Example – Abstract ClassesExample – Abstract Classes

BusTruckCar

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

VehiclecolormodelaccelerateapplyBrakes

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

Example – Concrete ClassesExample – Concrete Classes

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

TeacherDoctorStudent

programstudyYearstudyheldExam

Person

Example – Concrete ClassesExample – Concrete Classes

• Here, Car, Bus and Truck are concrete classes

BusCar

Vehicle

Truckcapacityloadunload

top related