Object-Oriented Programming

 < Day Day Up > 



For more than a decade, the object-oriented programming paradigm has been successfully implemented by the software industry through several supporting languages and operating systems. Many authors have discussed the concept extensively. However, this book also will make an attempt to discuss this concept as a service to readers who are fairly new to it, so that they do not have to look elsewhere for documentation while in the process of building their skills on the Linux platform. Although there are other object-oriented languages that were born prior to the C++ language, the unique feature of C++ is its close relationship with its predecessor C language, which has strong routes in the UNIX/Linux developer community. C++ is almost the de-facto standard language on the Linux and UNIX systems, if not other operating systems. Other languages—such as Java—are developed with some advanced features, but C++ is the main language of choice of system-level programmers. This is because it has some important advantages. C++ meets several requirements for operating system and device driver-level programming, compared to Java or other object-oriented languages. These features are derived from its predecessor C language. Therefore, this chapter covers the C++ language to a considerable extent. Object Pascal is the most recent addition to Linux, through the commercial tool Kylix delivered by Borland Software Corp. In fact, Kylix is a combined implementation of Borland’s version of C++ and Object Pascal (Delphi). Prior to the inception of Kylix, there were object-level libraries from other organizations, such as the K-Desktop development environment and the Qt library from Trolltech Corp. However, the introduction of Kylix to the Linux community has undoubtedly brought the Rapid Application Development (RAD) concepts to Linux. The Component Library for cross-platform development (CLX) is introduced by Kylix and is available with Delphi 6 and C++Builder 6™ onwards from Borland Software Corp. This means that CLX programs are source-level compatible between Windows and Linux platforms. Applications developed in Windows using the CLX library are portable at source-level to Linux and vice versa. This chapter discusses the CLX object model with respect to C++ and Object Pascal, and Chapter 6 will demonstrate the source-level portability of CLX applications between Windows and Linux. Although C++ and Object Pascal are both object-oriented languages, there are some differences between them. In order to make the CLX library unique for both languages, Borland made some language extensions to the core C++ language. These extensions are also part of this chapter’s discussion.

What Is an Object?

Before attempting to understand the concepts underlying the objects and their existence and interaction among different objects, it is a good idea to begin the discussion with an analogy. However, keep in mind that the analogy should be limited to the concepts that are being explained here. Every human being has certain common physical characteristics (although they are different from one another), such as having two eyes, two hands, two legs, a heart, a head, a brain, and so on. Therefore, there is a blueprint of a human being from which all of us are characterized. The human blueprint dictates that all humans should have a minimum set of characteristics. Similarly, an elephant blueprint dictates that all elephants should have some minimum characteristics, and so on. It is this human blueprint (or the elephant blueprint) that may be considered analogous to a class definition in an object-oriented language. A particular human being may be considered as an instance (or an object) of the human blueprint, or a particular elephant considered as being instantiated from the elephant blueprint. Similarly, an instance of the class is called an object of that class. The human beings (or the human objects in the analogy) are born at different times on a timescale and have different lifetimes (which is the duration of their existence in the world). Once the lifetime is over, the human beings cease to exist through the process of death. Likewise, the objects in an application are created (or instantiated) at different times (from different class definitions). Objects instantiated from the same class do not have to be considered the same, just as no two humans are considered the same; however, objects instantiated (from same/different classes) interact with each other during the lifetime of an application. In the real world, humans’ interaction with each other or with other animals and interactions of different animals with one another may be considered analogous to this concept.

This analogy can be further stretched to the object behavior patterns and state changes. During the lifetime of an application, an object’s state changes as time progresses or as a result of its interaction with other objects within the application (or sometimes external influences); this is termed as the object’s behavior pattern. Analogously, the humans’ behavior patterns also change with the passage of time or as a result of interaction with other humans, animals, or any other objects.

Encapsulation

Encapsulation is the concept of containment of an object’s data along with the object itself, similar to the human being having the ability to contain the knowledge acquired after being created within itself. The knowledge of an individual human being is exposed to the external world only through its behavior, which can be stimulated by external entities to tap the knowledge. Similarly, an object within an application exposes its data through its behavior, which is characterized by the methods operating on the object. These methods are invoked by other objects within the application or external objects. The core of the encapsulation concept is centered around the fact that the object’s data is not freely available to every object or any object. Rather, it is only exposed in a controlled way as specified in its class’s definition—through the access specifiers such as public, private, protected, and so on. Only members declared as public in a class are accessible to every other object in the application; those declared as private are only accessible to its own members, and those that are identified as protected are accessible to the classes derived from this class, in addition to providing access to its own members. Although the languages provide certain rules and guidelines for better object-oriented design, it is the responsibility of the designers and developers to design classes that produce robust objects that not only perform their tasks proficiently but also safeguard themselves and the applications housing them from abnormal terminations and undesired crashes.

Inheritance

Inheritance is the concept of creating derived class (or child class) definitions based on the base class (or the parent class). Inheriting a derived class from the base class makes it possible for the derived class to acquire some or all of the characteristics of the base class without any extra efforts. Based on our analogy, consider that a male human blueprint and a female human blueprint are derived from the core human blueprint and introduce their own characteristics in addition to the derived characteristics. Similarly, a derived class can add additional features that are not present in the base class, thus giving additional features to the ones that are acquired from the base class. The concept of inheritance can be extended to any number of levels of classes. Multiple inheritance is the concept of deriving one child class from more than one base class, thus acquiring the characteristics of both the base classes and still adding new features to the derived classes. However, the principle of multiple inheritance is not supported by every object-oriented language. For example, C++ permits inheriting a derived class from more than one base class, but languages such as Object Pascal and Java do not permit this feature; rather, they introduce the concept of interfaces. Therefore, with languages that support interfaces, the benefits of multiple inheritance are achieved by inheriting the derived class from one base class and implementing any number of interfaces. Thus, these languages have added a new dimension to the object-oriented technology. As Kylix is a product that attempts to implement a common object library for both C++ and Object Pascal, the Kylix implementation of C++ also supports interfaces along with the Delphi tool, which implements the Object Pascal language. More details on the concepts of interfaces are discussed in the later sections.

Polymorphism

Polymorphism is the ability of an object to show different behavior based on how the object is instantiated and accessed. In this process, the object can exhibit the behavior of one of its base classes (in the object’s class hierarchy) if the object is accessed through a pointer (or reference) to the corresponding base class. This means that we can make an object behave like one of its base class objects without altering any or some of its other characteristics. Our analogy works here also; a particular human being may exhibit different behavior at different times based on interaction with other human beings or any of the other objects in the world. Most of the time, we notice that humans exhibit behavior of one or more of their ancestors from whom they might have inherited some or all of their characteristics.

It is now time to put an end to the analogy with the hope that the three fundamental concepts of object-oriented programming will be discussed without just focusing on a particular language or implementation alone. The following sections are going to discuss these concepts with respect to the Object Pascal and C++ languages.



 < Day Day Up > 



Linux Application Development For The Enterprise
Linux Application Development For The Enterprise (Charles River Media Programming)
ISBN: 1584502533
EAN: 2147483647
Year: 2002
Pages: 86

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net