411 REFERENCE · Wikibooks
Introduction to Programming Languages/Object Oriented Programming
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Objects as Functions (Conceptual Model)
A useful abstraction:message → response Meaning:
You send a message (getData)
The object returns a response ("Hello")
Example — Java
class Node {
private String data;
private Node link;
public Node(String data, Node link) {
this.data = data;
this.link = link;
}
public String getData() { return data; }
}Here:
Message → getData()
Response → String
Same idea in SML (OO simulation)
datatype message = GetData | GetLink;
datatype response =
Data of string
| Object of message -> response;
fun node data link GetData = Data data
| node data link GetLink = Object link;The “object” is literally a function:message -> responseLimitation:
No strong compile-time checking for valid messages
OO in ML vs Java
MLJAVA Functional language
OO is not native
Can simulate OO Built-in OO support
But does NOT enforce OO style
Non-OO Programming in Java
Classes used as data structures
class Node {
public String data;
public Node link;
} Problems:
No encapsulation
Data is exposed
Behavior outside objects
static void add(Stack s, String data) {
Node n = new Node();
n.data = data;
n.link = s.top;
s.top = n;
}Problem:
Data and behavior are separated
Not truly OO