←Older revision | Revision as of 15:05, 17 October 2018 | ||
Line 1: | Line 1: | ||
[[wikipedia:Abstract_data_type|ADT]] stands for [[Abstract Data Type]] - an [[API]] design style where one knows operations for a type, but not their actual implementation. In contrary to [[OOP]], the right operation isn't derived from the actual object - rather it is associated with the whole algorithm that operates on provided data. | [[wikipedia:Abstract_data_type|ADT]] stands for [[Abstract Data Type]] - an [[API]] design style where one knows operations for a type, but not their actual implementation. In contrary to [[OOP]], the right operation isn't derived from the actual object - rather it is associated with the whole algorithm that operates on provided data. | ||
+ | |||
+ | In classical [[OOP]] each object comes with a virtual message table - a table of actual implementations of functions (e.g. messages) the object understands to and can react to. In a typed language like [[Java]] each non-final, non-static method of a class has an entry in the virtual method table. E.g. {{JDK|java/lang|Object}} shall have a table with following methods: | ||
+ | |||
+ | * '''toString''' | ||
+ | * '''hashCode''' | ||
+ | * '''equals''' | ||
+ | * '''clone''' | ||
+ | * '''finalize''' | ||
+ | |||
+ | The table is different for each class. It can point to the same or different implementation of the methods or even add additional entries into the table. When one calls such method, like '''toString''': | ||
+ | <source lang="java"> | ||
+ | static String message(Object who) { | ||
+ | return "Hello " + who.toString(); | ||
+ | } | ||
+ | </source> | ||
+ | the compiled code finds the virtual method table for object '''who''', locates the proper implementation of '''toString''' at the right index of the '''toString''' methods and only then invokes the implementation. In [[OOP]] operations are inherently associated with data - e.g. object. | ||
+ | |||
+ | === Abstract Data Types === | ||
+ | |||
+ | Some functional language (like [[Haskell]] also provide classes), but they do it differently. For example there may be a class '''Arithmetic''': | ||
+ | <source lang="haskell"> | ||
+ | class Arithmetic { | ||
+ | Aritmetic plus(Arithmetic, Arithmetic); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | [[TBD]] |