1 / 19

Inheritance

Explore the concepts of inheritance and polymorphism in object-oriented programming, including redefining, renaming, and deferred classes. Understand the use of inheritance for capturing high-level classes and defining architecture components.

cecilc
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance

  2. Inheritance: classRECTANGLE inheritPOLYGON • RECTANGLE includes features of POLYGON • r: RECTANGLE; p: POLYGON • Polymorphism • p := r -- legal assignment • Dynamic binding • p := r • p.perimeter -- perimeter of RECTANGLE is called Multiple Inheritance: classTA inheritTEACHER STUDENT

  3. Special syntax (1) Redefine: classRECTANGLE inheritPOLYGON redefine perimeter end feature perimeter : REAL is … end … end • Redefinition allows the feature clause of RECTANGLE to contain a new version of method perimeter

  4. Special syntax (2) Rename: classTA inherit TEACHER rename id as teacher_idend STUDENT rename id as student_idend feature … end • Several or none features can be renamed for each parent

  5. Example class TA inherit TEACHER -- first inherited class rename -- renaming features of TEACHER id as teacher_id, mark as get_mark redefine – redefining methods of TEACHER get_mark, -- need to use last defined name add_factor end STUDENT – second inherited class rename -- renaming of features of STUDENT id as student_id, mark as set_mark end …

  6. Abstract class deferred classDIGIT feature value : INTEGER -- current value lowest: INTEGER is-- lowest legal value deferred end end • A deferred class is a class that has at least one deferred feature • There is no need in redefinition for features inherited in deferred form

  7. The role of deferred class • Capturing high-level classes, with common behaviors. • Defining the components of an architecture during system design, without commitment to a final implementation. • Describing domain-specific concepts in analysis and modeling.

  8. Example deferred class STACK [G] feature -- Access count :INTEGERis-- Number of inserted elements deferred end item: Gis-- Last pushed element require not_empty: not empty deferred end;

  9. Example (cont) feature -- Status report empty :BOOLEAN is-- Is stack empty do Result :=(count = 0) end full :BOOLEAN is-- Is stack full deferred end feature – Element change put(x : G ) is-- Push x on top require not_full: not full deferred ensure not_empty: not empty pushed_in_top: item = x one_more: count = old count + 1 end

  10. Assertion Redeclaration rule In the redeclared version of a routine, it is not permitted to use a require or ensure clause. Instead you may: • Introduce a new condition with require else, for booleanorwith the original precondition. • Introduce a new condition with ensure then, for booleanandwith the original postcondition. In the absence of such a clause, the original assertions are retained.

  11. Example (1) deferred class A … foo (x :INTEGER ) is require r1 deferred … end end; class B inherit A … foo (x :INTEGER ) is require r2 do … end end; • The actual requirement is

  12. Example (2) deferred class A … foo (x :INTEGER ) is deferred … ensure e1 end end; class B inherit A … foo (x :INTEGER ) is do… ensure e2 end end; • The actual promise is

  13. Invariants Redeclaration rule • The invariant property of class is the boolean and of the assertions appearing in its invariant clause and of the invariant properties of its parents if any.

  14. Example class A … invariant i1 end; class B inherit A … invariant i2 end; • The actual invariant is

  15. Example (1) deferred class COURSE feature -- mark treatment average: INTEGER-- average of marks num_marks : INTEGER-- marks number add_mark(mark : INTEGER) is require legal_new_mark: mark >= 0 and mark <= 100 deferred ensure num_marks = old num_marks + 1 end

  16. Example (2) calc_factor (new_avg : INTEGER) : INTEGER is require legal_new_avg: new_avg >= 0 and new_avg <= 100 deferred ensure legal_calc_factor: average + Result = new_avg end invariant legal_avarage: average >= 0 and average <= 100 legar_num_marks: num_marks >= 0 end;

  17. Example (3) class LOGIC inherit COURSE creation feature add_mark(mark : INTEGER) is require legal_new_mark: mark >= 0 and mark <= 55 do if mark = 0 then average := mark else average := (average*num_marks + mark)//(num_marks + 1) end num_marks := num_marks + 1 end

  18. Example (4) calc_factor (new_avg : INTEGER) : INTEGER is require legal_new_avg: new_avg >= 0 and new_avg <= 55 do if average > 50 then Result := new_avg - average else Result := 0 end ensure legal_factor: Result <= 0 end invariant legal_avarage: average >= 0 and average <= 55 end;

  19. Example (5) class MAIN logic: LOGIC -- logic.add_mark(56) -- for now average >= 55 so LOGIC invariant violation logic.add_mark(46) logic.add_mark(56) io.put_line("Average is " + logic.average.out) -- average = 51 < 55 -- io.put_line("factor for average 55 is " + logic.calc_factor(55).out) -- factor = 4 so LOGIC ensure legal_factor violation io.put_line("factor for average 50 is " + logic.calc_factor(50).out) -- factor -1 logic.add_mark(40) -- average 47 -- io.put_line("factor for average 50 is " + logic.calc_factor(50).out) -- factor = 0 so COURSE ensure legal_calc_factor violation

More Related