Programming with Objects and Classes

From Lazarus wiki
Revision as of 21:51, 6 March 2009 by Roward (talk | contribs) (making this a summary page now with links to detailed topics)
Jump to navigationJump to search

Overview

FPC includes several language extensions to its "standard" Pascal syntax to support object oriented programming.

These extensions are described in the indicated chapters of the FPC Language Reference Guide: http://www.freepascal.org/docs.var. The Language Reference Guide includes syntax diagrams and further details not contained in this introductory tutorial. Of the four language features listed above, Objects and Classes form the basis of object oriented programming (OOP) in FPC and Lazarus. For those new to OOP, the Objects section includes more introductory concepts and the Classes section minimizes repetition by emphasizing the similarities and differences to the Objects syntax. In general, the Classes implementation seems to be more widely in use including Delphi ports and also Lazarus development. Often the term "objects" are used to refer to what is actually the "class" dialect of OOP in FPC. These documents will be worded to minimize any terminology confusion but outside of this document, the term "object" is often used to refer to "objects" created from a Class. In fact the FPC run time library (RTL) includes a class library with a base class called TObject.

Users migrating from Delphi may want to skip the section on Objects and go to the Classes section since the Classes version of FPC OOP is based on Delphi syntax. Be aware that some of the writeup in the Class section may refer to concepts from the Objects tutorial. Users familiar with the older Turbo Pascal OOP implementation may initially want to skip the section on Classes since the Objects implementation is based on the older Turbo Pascal dialect. For Mac developers familiar with the various Apple based Object Pascal dialects, neither the FPC Objects or Classes implementations provide a direct migration path. Currently, as of March 2009, there are discussions on the Mac Pascal Mailing list to potentially include some compiler support (new syntax) to provide access to Apple's Objective C / Cocoa framework.

General Concepts

OOP provides different ways to manage and encapsulate data and to manage program flow compared with other available programming language features and constructs. OOP often lends itself to modeling certain applications such as Graphic User Interfaces (GUI's) and physical systems in a more natural feeling manner. However OOP is not appropriate for all applications. Program control is not as explicit as the more basic Pascal procedural constructs. To obtain the most benefit from OOP, understanding of large class libraries is often required which can entail a steep learning curve. Maintaining large OOP application code has its advantages and disadvantages compared to maintaining strictly procedural code. There are many sources for learning OO analysis, design and programming techniques which are beyond the scope of this guide.

There are a lot of programming languages which incorporate OOP features as extensions or the basis of their language. As such, there are many different terms for describing OO concepts. Even within FPC, some of the terminology overlaps. In general, OOP usually consists of the concept of a programming object (or information unit) which explicitly combines and encapsulates a related set of data and procedural code. This data is usually persistent during program execution but without some of the problems inherent in declaring global variables. In addition, there are usually features which enable additional objects to be incrementally modified and/or extended based on previously defined objects which is usually referred to by the terms "inheritance and ploymorphism." Many OOP languages use the terms method or "message" referring to procedures which belong to an object. Much of the power of OOP is realized by late dynamic binding of methods at run time rather than at compile time. This dynamic binding of methods is similar to using procedural variables and procedural parameters but with greater syntactic cohesion and encapsulation with the data it is related to.

http://en.wikipedia.org/wiki/Object-oriented_programming

http://en.wikipedia.org/wiki/OOAD

http://en.wikipedia.org/wiki/Object_oriented_design