Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://ecomputernotes.com
Recap Inheritance
Derived
class inherits all the characteristics of the base class inherited characteristics, derived class may have its own unique characteristics benefit of inheritance is reuse
http://ecomputernotes.com
Besides
Major
(extension) (restriction)
Specialization
http://ecomputernotes.com
Generalization
In
OO models, some classes may have common characteristics extract these features into a new class and inherit original classes from this new class concept is known as Generalization
http://ecomputernotes.com
We
This
Example Generalization
Line
color vertices length move setColor getLength
Circle
color vertices radius move setColor computeArea
Triangle
color vertices angle move setColor computeArea
http://ecomputernotes.com
Example Generalization
Shape
color vertices move setColor
Circle
radius computeArea
Triangle Line
length getLength http://ecomputernotes.com angle computeArea
Example Generalization
Student
name age gender program studyYear study heldExam eat walk
Teacher
name age gender designation salary teach takeExam eat walk http://ecomputernotes.com
Doctor
name age gender designation salary checkUp prescribe eat walk
Example Generalization
Person
name age gender eat walk
Student
program studyYear study heldExam
Teacher
designation salary teach takeExam
Doctor
designation salary checkUp prescribe
want to add a new class to an existing model an existing class that already implements some of the desired state and behaviour the new class from this class and add unique behaviour to the new class
http://ecomputernotes.com
Find
Inherit
Sub-typing (Extension)
Sub-typing
means that derived class is behaviourally compatible with the base class compatible means that base class can be replaced by the derived class
Behaviourally
http://ecomputernotes.com
Person
name age gender eats walks
Student
program studyYear study takeExam
Shape
color vertices setColor move
Circle
radius computeCF computeArea
Specialization (Restriction)
Specialization
means that derived class is behaviourally incompatible with the base class incompatible means that base class cant always be replaced by the derived class
Behaviourally
http://ecomputernotes.com
age = a
Adult
age : [18..100] setAge( a )
NaturalSet
add( elem )
Overriding
A
class may need to override the default behaviour provided by its base class for overriding
Reasons
Provide behaviour specific to a derived class Extend the default behaviour Restrict the default behaviour Improve performance
http://ecomputernotes.com
Circle
radius draw computeArea
Triangle Line
length draw angle draw computeArea
Example Extension
Window
width height open close draw
DialogBox
controls enable draw
Example Restriction
IntegerSet
add( elem ) Add element to the set
If elem < 1 then NaturalSet give error else add( elem ) Add element to the set http://ecomputernotes.com
Circle
radius draw rotate
Abstract Classes
An
abstract class implements an abstract concept Main purpose is to be inherited by other classes Cant be instantiated Promotes reuse
http://ecomputernotes.com
Student
Here,
Teacher
Doctor
Car
Here,
Truck Bus
Concrete Classes
A
Main
Provides
http://ecomputernotes.com
Teacher
Doctor
Here,
Car
Bus
Truck
capacity load unload