I'm currently programming in an unnamed turing complete language which has support for pointers, primitive data types, structures, closures, and garbage collection, among other things.
I'm trying to use this language to implement an object oriented design pattern. So far, I have implemented the following algorithm:
make object (base class) subroutine:
create data structure
associate structure as an 'Object'
return data structure
make shape (extends from object) subroutine:
call make object
associate data structure as a 'Shape'
add 'getPerimeter' & 'getArea' subroutines to data structure
return data structure
make circle (extends from shape) subroutine:
call make shape
associate data structure as a 'Circle'
add 'getCircumference' as subroutine to data structure
re-define 'getPerimeter' & 'getArea' subroutines to data structure
return data structure
output shape area subroutine
assert shape is a Shape
print, call shape.getArea
This so far has worked great. Sub-classes correctly inherent the fields of their super-classes.
However, I am no longer able to take the extended classes and 'treat them as their super-class forms' anymore. For example, I can no longer treat a Circle
specifically as a Shape
, because when it was made into a Circle
it was augmented with Circle
member functions like getCircumference
. I cannot typecast it to be strictly a Shape
or an Object
, as its now been defined with sub-class functionality.
In Java terms, I can do this:
Fruit a = new Fruit(); // eat()
Apple b = new Apple(); // removeSeeds()
b.removeSeeds();
b.eat();
But I cannot do this:
Fruit a = new Apple();
a.removeSeeds(); // undefined
How can polymorphism be implemented in this sense?