Aaron Fude <aaronfude@gmail.com> writes:
if (hasMethod(obj, "toHTML"))
print(callMethod(obj), "toHTML"));
else
print(obj.toString());
The OO alternative would be to create an interface "HTMLable", but the
Object oriented languages (like Smalltalk or Lisp) do not
necessarily have the concept of ??interface?? at all. They lack
Java's static type system and, therefore, they do not need
interfaces. So, your idea of ??OO?? seems to be restricted to
Java's interpretation of ??OO??. In Java, reflection is used to
add some of the dynamics to Java that object-oriented
languages have right from the start.
When an object does not have a ??toHTML?? operation, you can not
use ??toString?? instead because it might evaluate to a string
like "<" that might result in an HTML document that is not
well-formed. There is no way to get an HTML representation
from an object that is not aware of HTML.
Your code is not object-oriented, but this is not because it
does not use an interface or because it uses reflection. It is
not object-oriented because it uses a type-branch instead of
polymorphism. To implement polymorphism in Java, sometimes an
interface is used. So this has to do with interfaces only
indirectly.
Generally, OP, reflection adds complexity to a solution, and reduces safety.
solution. Reflection exists as a tool because it is sometimes useful, more
useful than other available solutions. Like many powerful tools, reflection
tricks of the trade.
knife if you find its edge too sharp.