|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Comments In step 1, you declared a Vehicle thing to serve as a base thing. This base thing functions as a template for all subsequent descendents of Vehicle; you do not intend to create objects of type Vehicle. This is not always the case when defining classes for an application. You might find that defining a lone thing suffices to represent some entity. Do not use inheritance just for the sake of using inheritance. No requirement of the object-oriented paradigm states you must always create an inheritance tree. It is important to remember that you should only provide a minimal interface for a thing. An overabundant interface is as bad a design as an inadequate interface. 2.3 Learn the concept of encapsulation?Problem I want to extend my knowledge of object-oriented concepts. I am familiar with inheritance and am ready to learn about encapsulation. I hear encapsulation is an important principle of object-oriented programming and I want to take advantage of its traits. Technique As with any programming concept (especially object orientation), the technique required is quite easy. The technique is to begin with something simple and build upon that. You will continue the use of the definitive language outlined in How-To 2.2. In the following Steps section, you will use the Vehicle thing from How-To 2.2. Vehicles have various (physical) parts, so those parts will be described by the parts keyword. Steps
How It Works Encapsulation specifies the containment of functionality (services) and parts within a thing. The concept specifies that you should hide parts within a thing. Only the thing should know about its internal parts. Another aspect of encapsulation is maintaining responsibility for a things parts and services. Each thing has a contract stating a responsibility to its clients. A thing should be able to maintain its own existencethat is, to stand on its own. A thing is considered cohesive if it can fully encapsulate itself. You must remember to protect the internal state of your objects; if you dont, those objects will become unstable or corrupt. An unstable object is untrustworthy; you can never count on the state of a corrupt object. Comments Inheritance and encapsulation are very important with respect to object-oriented principles. However, they are but two important concepts. A third concept, namely polymorphism, is addressed in the next How-To. 2.4 Learn the concept of polymorphism?Problem I have a good understanding of inheritance and encapsulation and feel I have to grasp the concept of polymorphism to round out my knowledge of object-oriented technology. Technique Of the three object-oriented principles, polymorphism is probably the most difficult to comprehend. Effective polymorphic behavior relies on proper implementation of inheritance and encapsulation. The technique for ensuring effective polymorphic behavior is to fully understand its intent and when polymorphism should and should not be used. Returning to the Vehicle example from previous How-Tos, lets apply polymorphism to the picture. Steps
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|