My first introduction to programming was Java, which was horrible so I learnt Actionscript3 which was nice... "The point is" OOP was my introduction to programming where by I went on to gain a career in PHP and while I can confidently write programs/scripts in imperative languages I am no where near as comfortable in doing so.
The primary reason is because I am unable to understand how imperative/procedural languages can be extended in the same way OOP languages use polymorphism and substitution.
I am now looking to learn additional languages and may even have a crack at C, only if I can work out the basic principles for substituting functionality.
Can anyone explain the basic principles used to extend the functionality of a library/function without re-writing that entire function. Because in OOP you would override one method of a class and possibly call the super method to retain some of the original behavior.
class
es are not objects, they're abstract data types. C can do abstract data types easily enough with opaque structs. Instances of Javainterface
s are objects (in the OOP sense), and there's no safe way of implementing them in C, because C doesn't have closures. So you can make a struct of function pointers, but you have nowhere to put... – Doval Oct 31 '14 at 17:04void
pointer to the struct which points to where the instance data is, but then you have to cast to get the data and risk undefined behavior. – Doval Oct 31 '14 at 17:05struct
which holds it all together is calledkobject
. – Jörg W Mittag Oct 31 '14 at 17:26