1

InputStream is an Abstract class.Then how are we able to access System.in.And moreover int read() is an abstract method in InputStream class.Then how are we able to access System.in.read() method if read() is an abstract method. Like

int a=System.in.read();
System.out.println((char)a); 
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Sourav Deb
  • 119
  • 8
  • maybe have a look at [this](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)... – Lino Jul 28 '17 at 10:13
  • You should check the initializeSystemClass() method in System class which contains the details of how these Streams are initialized. – dvsakgec Jul 28 '17 at 10:15
  • I don't think that this is a bad question. One shouldn't down vote it. – Joshua Jul 28 '17 at 10:16
  • It is an instance of `InputStream` and whatever concrete class the implementation provides. This is a common enough idiom that you shouldn't have to ask about it specifically for `System.in`. – user207421 Jul 28 '17 at 10:17

3 Answers3

3

I suggest you to read more about abstraction and inheritance methods in Java.

If you extend an abstract class, you have to implement its abstract methods. This way, you provide an implementation for it, which can be called by consumers.

System.in is an instance of a class which extends InputStream, not of InputStream directly.

Joshua
  • 2,932
  • 2
  • 24
  • 40
2

is System.in an object reference of InputStream class?

yes!, it is declared/documented in the System class:

/**
 * The "standard" input stream. This stream is already
 * open and ready to supply input data. Typically this stream
 * corresponds to keyboard input or another input source specified by
 * the host environment or user.
 */
public final static InputStream in = null;

but at runtime is a reference to a BufferedInputStream class

so ou are not instantiating an abstract class enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

InputStream is an abstract class which cannot be instantiated directly. System.in refers to the object of type InputStream which means that System.in refers to the object of that class which extends the InputStream class.

For example

abstract class IAmAbstract{
// ...
}

class IAmNotAbstract extends IAmAbstract{
// ...
}

Of course, the following statement is correct:

IAmNotAbstract obj = new IAmNotAbstract();

As well as this statement is also correct:

IAmAbstract obj = new IAmNotAbstract();

So, any object of subclass of InputStream is also a type of InputStream class and subclass itself.

Stack Overflow
  • 1
  • 5
  • 23
  • 51