Inheritance

Summary

One of the greatest strengths of an object-oriented programming environment is the power of inheritance. Every object in Cold has a "parent," or an object on which it was based on. The new object is then said to be the "child" of the "parent." Inheritence, simply put, is the idea that the child will inherit certain traits and behaviors from the parent.

Variable Inheritence

Summary

Cold objects inherit the functionality of their parents, but not all of the properties. Keep in mind that an object variable may only be read or written by methods defined on the exact same object that the variable is defined. This means that code defined on descendants may not directly access variables defined on ancestors.

Example

Let's create a $thing named Test1, then create a child of Test1 named Test2. We will define a variable on Test1 called var_test, then we will define a method on Test2 called get_var_test, which will attempt to read Test2's var_test value.

@new $thing named Test1
@rename Test1 to $test1

@av $test1,var_test = 1

@new $test1 named Test2
@rename Test2 to $test2

@program $test2.get_var_test +access=public
	return var_test;
.

If you follow the above example, you will find yourself with an error when you attempt to execute Test2.get_var_test():

;$test2.get_var_test()

However, if we instead define the verb on the same object that var_test was defined on ($test1), then we'll see that this works better... and can also be used effectively on $test2, since it will inherit the behavior of $test1.

@dm $test2.get_var_test

@program $test1.get_var_test +access=public
	return var_test;
.

Now try to execute .get_var_test() on both $test1 and $test2...

;$test1.get_var_test();
;$test2.get_var_test();

You will see that both calls executed properly -- until you look more closely at the values that were returned. The first call (on the parent) returned the value that you set when you created the variable... but the call on the child, $test2, returned 0. This is because object variable values are encapsulated across generations. The value of the parent will not be inherited by the next generation, nor vice versa.