Can you please clarify to me how ot understand abstract class type (for example System.in is of type InputStream) as a class (or instance for that tmatter) variable. What is behinnd "ataching" System.in to the keybpard?
Thanks a lot, Lorana
author and iconoclast
System.in points to an object that implements the InputStream interface. The name of that class is unimportant and implementation-dependent. It will likely either contain some native methods to hook up to standard input, or it will access other internal classes that do.
This sort of trick -- a variable of interface type, or method returning interface type, backed by an instance of some implementation-dependent class -- is very common in the JDK. The entire database access API JDBC (in the java.sql package, have a look!) is based on this idea, for example. You onl deal with interfaces; behind the scenes, the various database vendors can implement those interfaces with classes whose names you don't need to know.
A minor correction: InputStream is an abstract class, not an interface. But the general principle is the same. The variable System.in is declared to be of a general type, InputStream, and the actual object is of a more specific type, but you generally don't need to know what that type is. If you're curious, you can execute
to see what exact class you get. But for the most part, we don't need to know or care about those details.
"I'm not back." - Bill Harding, Twister
Joined: May 05, 2008
Thank you so much Ernest and Jim, your answers actually cleared many things at once for me. Thank you, Loranja