System.in , System.out Abstarct type as class variable
1 posts in topic
Flat View  Flat View

Posted By:   Lorana_Berd
Posted On:   Monday, May 5, 2008 06:22 PM

Hi Guys,

Can you please explain to me System.in and System.out
I am confused about the type of the System.in it is InputStream which is abstract class. I guess I am confused about abstarct class as class variable (or instance variable also). Please help me to clarify this.

Thanks a lot,

Re: System.in , System.out Abstarct type as class variable

Posted By:   Stephen_McConnell  
Posted On:   Tuesday, May 6, 2008 01:58 PM

The whole thing of abstract vs concrete classes and interfaces in classes can be confusing when you are first starting out.

You have to understand the concept of inheritance to start getting a grip on it.

InputStream is indeed an Abstract class. What this means is that there are some methods in InputStream that a child class of it needs to implement.

In the case of InputStream these are markSupported() and read().

This also means that you cannot create an instance of InputStream. The cool thing, however is any concrete instance of a child class of InputStream can be cast as InputStream because it has to implement those methods.

When you access System.in it actually is a System Default instance of a concrete type object that implements ALL the methods of InputStream. You really don't care what the real object type is.... just as long as it can be accessed by the methods of InputStream.

You can create a variable of type InputStream and set the output of System.in to it. or you can just access System.in directly.

int inChar = 0;

while((inChar = System.in) != -1){

.... process your character.
.... you could build a String with StringBuffer
and add the character to the stringbuffer...

This is a pain in the rear and there are easier ways to do this, but this gives you an idea of what might be going on.

Hope this helps.

Stephen McConnell

About | Sitemap | Contact