The moose likes Java in General and the fly likes System.in - abstract type class variable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "System.in - abstract type class variable" Watch "System.in - abstract type class variable" New topic
Author

System.in - abstract type class variable

Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
Hello Guys,

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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

Hi,

Welcome to JavaRanch!

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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
Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
Thank you so much Ernest and Jim, your answers actually cleared many things at once for me.
Thank you,
Loranja
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: System.in - abstract type class variable
 
Similar Threads
abstract question!!
InputStream
System.in.read() - Abstract method?
What exactly is System.in?
System.in an object ...how ??