Difference between revisions of "Programming Using Objects Page 2"

From Lazarus wiki
Jump to navigationJump to search
Line 42: Line 42:
 
Note that there is no ''Draw'' method declared for ''TRectangle'' and the inherited method from ''TShape'' will be called implicitly.  When the ''TSquare.Draw'' method is executed, it overrides the ''TShape.Draw'' method but after calling the writeln statement, it invoking  the '''inherited''' ''TShape.Draw'' method.  Then, after the inherited method returns, ''TSquare.Draw'' does some post processing.  The '''inherited''' keyword is often useful in constructors for pre processing task like creating and initializing auxiliary objects and additional  fields associated with the object.  Similarly, the keyword is also useful in destructors for freeing up auxiliary objects and data structures the object previously created..  It should be noted that allthough code reuse and encapsulation is improved, program flow can be more difficult to follow than for straight procedural code.
 
Note that there is no ''Draw'' method declared for ''TRectangle'' and the inherited method from ''TShape'' will be called implicitly.  When the ''TSquare.Draw'' method is executed, it overrides the ''TShape.Draw'' method but after calling the writeln statement, it invoking  the '''inherited''' ''TShape.Draw'' method.  Then, after the inherited method returns, ''TSquare.Draw'' does some post processing.  The '''inherited''' keyword is often useful in constructors for pre processing task like creating and initializing auxiliary objects and additional  fields associated with the object.  Similarly, the keyword is also useful in destructors for freeing up auxiliary objects and data structures the object previously created..  It should be noted that allthough code reuse and encapsulation is improved, program flow can be more difficult to follow than for straight procedural code.
  
== Objects - Public, Private, Protected ==
+
== Objects - Abstract Methods ==
 
 
 
 
  
 
== Objects - Static Methods ==
 
== Objects - Static Methods ==
  
  
 
+
== Objects - Public, Private, Protected ==
 
 
== Objects - Abstract Methods ==
 

Revision as of 23:46, 8 March 2009

Objects - Self & Inherited

Self Keyword

Objects have an implicit parameter, self, which can be used in method calls which can be used to qualify fields and methods of the object. The explicit use of the keyword is optional but provides clarity if desired. The following implicit and explicit use of the self qualifier for field and method access within a method are identical. To illustrate this, let's say the the following (non useful) code was inserted into the Rectangle.Draw method:

<delphi> procedure Rectangle.Draw; begin

Self.x := 1; x := 1;

Self.Getparams; GetParams;

end; </delphi>

Inherited Keyword

A bit more useful is the keyword inherited for use within an object's methods. By using this keyword, one can explicitly call the parent method of the same name. This is useful because it allows a method overriding it's parent's methods the capability of using the parent's code before or after child object's code is called. The use of the inherited keyword can propagate up and inheritance chain. For our simple example program, there is input code common to all objects; the readln statements. Previously, this code had to be duplicated into the TSquare.GetParams method since it overrode the behavior of it's anscestor object types TRectangle and TShape. Now, using the inherited keyword, the GetParams method for TShape and TSquare can be implemented in the following manner to provide more efficient reuse of code.

<delphi>

procedure TShape.GetParams;

 begin

readln(x, y, width, height); writeln;

 end;
procedure TSquare.GetParams;
 begin

write('TSquare.GetParams : '); inherited; height := width; writeln('making sure all sides are equal for Square');

 end;

</delphi>

Note that there is no Draw method declared for TRectangle and the inherited method from TShape will be called implicitly. When the TSquare.Draw method is executed, it overrides the TShape.Draw method but after calling the writeln statement, it invoking the inherited TShape.Draw method. Then, after the inherited method returns, TSquare.Draw does some post processing. The inherited keyword is often useful in constructors for pre processing task like creating and initializing auxiliary objects and additional fields associated with the object. Similarly, the keyword is also useful in destructors for freeing up auxiliary objects and data structures the object previously created.. It should be noted that allthough code reuse and encapsulation is improved, program flow can be more difficult to follow than for straight procedural code.

Objects - Abstract Methods

Objects - Static Methods

Objects - Public, Private, Protected