Re: Design question - methods calling methods
Please trim your posts.
Rhino wrote:
Right now, I think I would benefit from the answer to a new mystery.
Should have a new thread, then.
I do database stuff fairly frequently and mostly use my copy of DB2 as my
database engine. Naturally, I use ResultSets for a lot of this work. I
know that ResultSet is an interface, not a class, but it is rich in
exactly the sorts of methods I need to grab the Strings, integers and
whatnot in the database or to write them to the database. A short time
ago, I was looking at the source for ResultSet and found that not one of
the many methods in that interface are implemented; every single method
seems to be abstract. I see in the JavaDoc that there are a number of
"sub-interfaces" - I'm really not sure what a sub-interface is and how it
is different from a interface and that it has a "super-interface" -
again, I'm not sure what that is! - but the perplexing thing is that I am
not explicitly using the super-interface or any of the sub-interfaces yet
my code still works fine. That kind of thing disorients me; I wonder how
that could possibly be working and why I don't need to change from
ResultSet to a real class or another sub-interface or whatever. My
pragmatic self is capable of just ignoring all of that and saying "It's a
mystery but I'm not going to question it since it obviously works." But
my more idealistic self is bothered that it isn't obvious to me why the
ResultSet works despite its troubling aspects.
Can you possibly shed some light on what is happening there?
Yes.
OK, I will actually do so.
'ResultSet' is not implemented in the standard Java API, but in the JDBC driver.
The instantiating class is always a subtype of 'ResultSet' specific to that
driver.
The details are hidden by the driver library. That's the WHOLE POINT of
interface-driven programming (a subset of type-driven programming)!
ALL you need to know is the interface! You want to AVOID the impulse "to
change ... to a real [sic] class or another sub-interface or whatever".
'ResultSet' works, like all interface-declared variables, because the variable
always points to an actual object which is an instance of a class.
VARIABLES ARE POINTERS!
The actual action happens in the object to which the variable points.
This is the heart and soul of polymorphism - you refer (point) to a supertype,
but the behavior is enacted by an instance of the subtype.
All the variable does is give you access to the behavior through the public
members of the variable's type. That access is always to the actual behavior
of the instance.
VARIABLES ARE POINTERS!
All your code cares about is that 'ResultSet' has certain behaviors. Those
are the behaviors promised by the type.
That way, when you change databases, say from (blecch!) MySQL to (ahhhh!)
PostgreSQL, you don't have to rewrite your code much. You still have a
'ResultSet' whose members are the members your code is already using; at run
time the JDBC driver returns a pointer to the actual instance to the
'ResultSet' variable.
VARIABLES ARE POINTERS!
--
Lew