1

I read that the InputStream class,which is the superclass for all byte-stream input classes, is an abstract class which is inheritted by other specific byte-stream input classes. However, I also read that in java.lang.System there is an object that refers to the standard input stream which we refer to as java.lang.System.in and that object instantiates the class InputStream.

In Sun API documentation, "in" is defined as:

public static final InputStream in

But i know that abstract classes cannot be instantiated. Have I misunderstood something?

  • It's the difference between the left- and right-hand sides of a declaration and assignment. You can declare abstract and interface types; that's the static compile time type of a reference. The right-hand side expresses the dynamic, runtime type of the object on the heap. – duffymo Aug 17 '15 at 09:21
  • Quick test `System.out.println(System.in.getClass());` and you will know it's actual class. – Codebender Aug 17 '15 at 09:23
  • Thanks a lot! I get how it is now. I should have looked into it more. But just asking for a clarification: "Is `java.lang.System.in` set to be a reference to `java.io.BufferedInputStream` by default or can this change?" – Themistoklis Haris Aug 17 '15 at 09:45

1 Answers1

1

Variable "in",here, just holds a reference to an Object that can fullfill the requirement of "is a InputStream". That means that variable "in" can hold any instante of a concrete Class that extends InputStream as for example AudioInputStream, which fulfill the condition "is a InputStream". It's something related to the polymorphism I think

dskfdskjgds
  • 341
  • 3
  • 14