Re: enumerate the consumers of foo.toString() within an application
Rex Mottram <rexm@not.here> writes:
Say I have a class with an explicit toString() method, for which I want
to change the output format (have it return a different String).
You can write toString() as you like it.
This turns out to be harder than it I thought. For one thing, toString
is special in that it can be called implicitly, i.e. out.println(foo) is
the same as out.println(foo.toString()).
It is not strictly the same - it just might have the same effect.
?toString()? is not special compared to any other method.
ideas on how to enumerate the users of foo.toString() within a
self-contained application?
You need to inspect all libraries you use for uses, too,
because of run-time polymorphism in Java. Let me elaborate ...
What is object-oriented programming?
Object-oriented programming uses objects (storage entities)
annotated with run-time types: Every object carries
information about its type, which implies information about
the layout and encoding of its storage.
What is an object-oriented programming language?
An object-oriented programming language supports
object-oriented programming, but it usually hides the type
information of objects and automatically assigns operations
specified for a type to each object. Thus, in an object
oriented programming language, the programmer does not read
the type information himself nor does he use it to select an
appropriate action. Instead he just states the action by a
"verb" with a type and the programing language will then
make sure, that the correct implementation is called for any
given object at run-time. This is called ?run-time
polymorphism? or ?late binding?. Individual object-oriented
programming languages might include additional support for
object-oriented programing, but this binding is the core
feature of every object-oriented programming language.
What is the main advantage of an object-oriented programming
language?
The main advantage is that verbs can be extended without the
need to modify existing code. For example, in Java, a new
type can extend the verb ?toString?. This extension can then
be used immediatly by the existing ?println? verb, without
the need that the author of the code for ?println? was aware
of the new type, so it is not necessary to modify the given
Java SE library, which contains other definitions of the
verb ?toString?. One only needs to /add/ new code. Thus, the
?open/closed principle? is fulfilled.
Can you give a small example programm that shows something
that can be done in an object-oriented programming language,
but is not possible in a purely procedural programming
language?
Yes, a Java program illustrating the answer to the previous
question is:
class Position
{ int x; int y; public java.lang.String toString()
{ return java.lang.String.valueOf( x ) +
", " + java.lang.String.valufOf( y ); }}
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( new Position() ); }}
The output of this program is as follows.
0, 0
What is the reason for run-time information?
It is needed whenever data, whose encoding, layout or
language might change or is not yet known, is to be
transfered between two parties to indicate this encoding,
layout or language.