Computer Notes - Inheritance

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Inheritance

    1/26

    InheritanceInheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    2/26

    RecapRecap InheritanceInheritance

    Derived class inherits all the characteristics ofDerived class inherits all the characteristics of

    the base classthe base class

    Besides inherited characteristics, derived classBesides inherited characteristics, derived classmay have its own unique characteristicsmay have its own unique characteristics

    Major benefit of inheritance is reuseMajor benefit of inheritance is reuse

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    3/26

    Concepts Related with InheritanceConcepts Related with Inheritance

    GeneralizationGeneralization

    SubtypingSubtyping (extension)(extension)

    Specialization (restriction)Specialization (restriction)

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    4/26

    GeneralizationGeneralization

    In OO models, some classes may have commonIn OO models, some classes may have common

    characteristicscharacteristics

    We extract these features into a new class andWe extract these features into a new class andinherit original classes from this new classinherit original classes from this new class

    This concept is known as GeneralizationThis concept is known as Generalization

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    5/26

    ExampleExample GeneralizationGeneralization

    Circlecolor

    verticesradius

    move

    setColor

    computeArea

    Line

    colorvertices

    length

    movesetColor

    getLength

    Trianglecolor

    vertices

    angle

    move

    setColor

    computeArea

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    6/26

    ExampleExample GeneralizationGeneralizationShape

    color

    vertices

    move

    setColor

    Circleradius

    computeArea

    Linelength

    getLength

    Triangleangle

    computeArea

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    7/26

    ExampleExample GeneralizationGeneralization

    Teachername

    age

    gender

    designationsalary

    teach

    takeExam

    eat

    walk

    Studentname

    agegender

    program

    studyYear

    studyheldExam

    eat

    walk

    Doctorname

    agegender

    designation

    salary

    checkUpprescribe

    eat

    walkhttp://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    8/26

    ExampleExample GeneralizationGeneralizationPerson

    name

    agegendereat

    walk

    Teacherdesignation

    salary

    teach

    takeExam

    Studentprogram

    studyYear

    study

    heldExam

    Doctordesignation

    salary

    checkUp

    prescribe

  • 8/3/2019 Computer Notes - Inheritance

    9/26

    SubSub--typing & Specializationtyping & SpecializationWe want to add a new class to an existingWe want to add a new class to an existing

    modelmodel

    Find an existing class that already implementsFind an existing class that already implementssome of the desired state andsome of the desired state and behaviourbehaviour

    Inherit the new class from this class and addInherit the new class from this class and adduniqueunique behaviourbehaviour to the new classto the new class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    10/26

    SubSub--typing (Extension)typing (Extension)

    SubSub--typing means that derived class istyping means that derived class is

    behaviourallybehaviourally compatible with the base classcompatible with the base class

    BehaviourallyBehaviourally compatible means that base classcompatible means that base classcan be replaced by the derived classcan be replaced by the derived class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    11/26

    ExampleExample

    SubSub--typingtyping(Extension)(Extension)

    Person

    nameage

    gender

    eatswalks

    Student

    programstudyYear

    study

    takeExam

  • 8/3/2019 Computer Notes - Inheritance

    12/26

    ExampleExample

    SubSub--typingtyping(Extension)(Extension)

    Shapecolor

    vertices

    setColormove

    Circle

    radiuscomputeCF

    computeArea

  • 8/3/2019 Computer Notes - Inheritance

    13/26

    Specialization (Restriction)Specialization (Restriction)

    Specialization means that derived class isSpecialization means that derived class is

    behaviourallybehaviourally incompatible with the base classincompatible with the base class

    BehaviourallyBehaviourally incompatible means that baseincompatible means that baseclass canclass cant always be replaced by the derivedt always be replaced by the derived

    classclass

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    14/26

    ExampleExample SpecializationSpecialization

    (Restriction)(Restriction)Person

    age : [0..100]

    Adultage : [18..100]

    setAge( a )

    setAge( a )

    age = a

    If age < 18 thenerror

    else

    age = a

  • 8/3/2019 Computer Notes - Inheritance

    15/26

    ExampleExample SpecializationSpecialization

    (Restriction)(Restriction)

    IntegerSet

    NaturalSet

    add( elem )

    add( elem )

    add element

    to the set

    If elem < 1 then

    errorelse

    add element

    to the set

  • 8/3/2019 Computer Notes - Inheritance

    16/26

    OverridingOverriding

    A class may need to override the defaultA class may need to override the defaultbehaviourbehaviour provided by its base classprovided by its base class

    Reasons for overridingReasons for overriding ProvideProvide behaviourbehaviour specific to a derived classspecific to a derived class

    Extend the defaultExtend the default behaviourbehaviour Restrict the defaultRestrict the default behaviourbehaviour

    Improve performanceImprove performance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    17/26

    ExampleExample SpecificSpecific BehaviourBehaviourShape

    color

    verticesdraw

    move

    setColor

    Circleradius

    draw

    computeArea

    Linelength

    draw

    Triangleangle

    draw

    computeArea

  • 8/3/2019 Computer Notes - Inheritance

    18/26

    ExampleExample ExtensionExtensionWindow

    width

    height

    open

    close

    draw

    DialogBoxcontrolsenable

    draw

    1- Invoke Windowsdraw

    2- draw the dialog

    box

  • 8/3/2019 Computer Notes - Inheritance

    19/26

    ExampleExample RestrictionRestrictionIntegerSet

    NaturalSet

    add( elem )

    add( elem )

    Add element to

    the set

    If elem < 1 then

    give error

    elseAdd element to

    the set

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    20/26

    ExampleExample Improve PerformanceImprove Performance

    Class Circle overridesClass Circle overridesrotaterotate operation of classoperation of class

    Shape with a NullShape with a Null

    operation.operation.

    Shapecolor

    coorddraw

    rotate

    setColor

    Circleradius

    draw

    rotate

  • 8/3/2019 Computer Notes - Inheritance

    21/26

    Abstract ClassesAbstract Classes

    An abstract class implements an abstractAn abstract class implements an abstractconceptconcept

    Main purpose is to be inherited by other classesMain purpose is to be inherited by other classesCanCant be instantiatedt be instantiated

    Promotes reusePromotes reuse

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    22/26

    ExampleExampleAbstract ClassesAbstract Classes

    TeacherDoctorStudent

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

    Personname

    age

    gender

    eat

    walk

  • 8/3/2019 Computer Notes - Inheritance

    23/26

    ExampleExampleAbstract ClassesAbstract Classes

    Bus TruckCar

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

    Vehicle

    colormodelaccelerate

    applyBrakes

  • 8/3/2019 Computer Notes - Inheritance

    24/26

    Concrete ClassesConcrete ClassesA concrete class implements a concrete conceptA concrete class implements a concrete concept

    Main purpose is to be instantiatedMain purpose is to be instantiated

    Provides implementation details specific to theProvides implementation details specific to the

    domain contextdomain context

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Inheritance

    25/26

    ExampleExample

    Concrete ClassesConcrete Classes

    Here, Student, Teacher and Doctor areHere, Student, Teacher and Doctor are

    concrete classesconcrete classes

    TeacherDoctorStudent

    programstudyYear

    study

    heldExam

    Person

  • 8/3/2019 Computer Notes - Inheritance

    26/26

    ExampleExample

    Concrete ClassesConcrete Classes

    Here, Car, Bus and Truck are concrete

    classes

    BusCar

    Vehicle

    Truckcapacityload

    unload