09: OOP Concepts: Encapsulation & Polymorphism
In this lesson I will make my final stand on demystifying the OOP Concepts: Encapsulation & Polymorphism. In this lesson, you will learn how to provide a limited amount of these two features by how you define a Function/Methods Parameter set.
Encapsulation is protecting an object's properties from being manipulated outside one of its own methods. Using the Label Convention and OBP Rules provide a great amount of Encapsulation. However, lets consider Class Globals declared with the Global keyword. Globals are accessible to all code in the program. This may not be desireable. To protect Class Globals from outside methods and constucts, we could create a object named objectGlobal in which its properties serve as Globals for the class.
We can also use Class Global Parameters based on Local Variable Scope in which Local variables can only be used within the function they are created in. The Global keyword is not used. Instead, the Class Global Parameter only exists as a parameter defined in all the object methods that use it. The Class Global Parameter is strictly passed between only a object's methods, completely isolating it from outside methods and constructs. Although you can have upto 255 parameters in a function, using a large number Class Global Parameters is not practical.
Polymorphism is a OOP Language's ability to process objects differently depending on their datatype. Its accomplished by Function Overloading, which is the ability to use a single Function label for multiple purposes. The closest feature we have to Function Overloading in Blitz3D is the ability to assign default values to function parameters. I refer to these as Optional Parameters. By using Optional Parameters we either override the default value by passing an argument, Or omit the argument from the Function call.
Placing Function parameters in a specific Left-to-Right order is key to effeciently using default values to imitate Overloading. Only basic datatypes (integer, floats, strings) can be assigned default values. Object Pointers cannot be assigned default values. Object Pointers are placed on the LEFT of Regular Parameters. Regular Parameters are placed on the LEFT of Optional Parameters. We maximize effeciency, by ordering Optional Parameters based on how often their default values are overridden. Optional Parameters that are most often overridden are placed to the LEFT of Optional Parameters that are least often overridden.
- this pointer - first parameter
- Object Pointer
- Regular Parameter
- Optional Parameter (most often overidden)
- Optional Parameter (least often overidden)
Function objectPurpose(this.object, object pointers, regular parameters, optional parameters)
invaderrelative%=False (least often overridden)
invaderz#=0.0 (most often overridden)
Order the parameters Left-to-Right based on their Default Values.
Email-in this Exercise for Grade!