-1
  1. The declaration of System.in says it has type InputStream:

    public static final InputStream in
    

    But InputStream is an abstract class.

    By default what is the actual type of in? Is it a concrete subclass of InputStream?

    Can we change the type of the object which in refers to? I guess no, because of final?

  2. Similarly the declaration of System.out says it has type PrintStream:

    public static final PrintStream out
    

    Why is out declared to have a concrete class type, while in isn't?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tim
  • 1
  • 141
  • 372
  • 590
  • There is a concrete subclass. I don't think it's a _public_ concrete subclass. – Louis Wasserman Nov 18 '17 at 23:52
  • @Oliver why is it duplicate? Do you notice that the linked question only talks about `out` and a different question? Do you read both posts? If not, what your did here is very inappropriate and cause a lot of damage, and would you correct your mistake of closing my post? – Tim Nov 18 '17 at 23:54
  • 2
    Your question(s) are the intersection of a number of existing questions - I've added another duplicate just now. – Oliver Charlesworth Nov 19 '17 at 00:03
  • @Oliver:Do you read the two linked posts to find answers to my remaining questions: "By default what is the actual type of in? Is it a concrete subclass of InputStream?" and "Why is out declared to have a concrete class type, while in isn't?"? If you can't find exact duplicates for my post, why would you claim mine is duplicate? – Tim Nov 19 '17 at 00:04
  • 2
    @Tim, 1. you can check a runtime class by `System.in.getClass()` (it gives `class java.io.BufferedInputStream` for me) – Andrew Tobilko Nov 19 '17 at 00:05
  • 3
    This is exactly why the convention is to have one question per question... (It also seems that you already know the answer to one of your subquestions, as it was provided in an answer to your earlier [equivalent question](https://stackoverflow.com/questions/47367845/) about C#) – Oliver Charlesworth Nov 19 '17 at 00:06
  • @OliverCharlesworth Would you like me to post multiple posts for very similar questions in a short amount of time? Do you want to claim they are duplicate with each other again? What is Java question to do with C# questions? Do you find it reasonable to claim my java question is duplicate of my C# question? – Tim Nov 19 '17 at 00:08

1 Answers1

1

It's a combination of:

  1. Historical reasons
  2. Flexibility of concrete type (e.g. according to implementation and/or runtime environment) and only offering the methods required (a.k.a. "program to an interface, not to a concrete class")

1, historical reasons: The classes XxxxStream work in bytes, while XxxxWriter work on chars, right? Not quite. There are exceptions. System.out is a PrintStream, which works on chars! Why isn't it a PrintWriter. Historical reasons.

2, programming to an interface. System.out is declared as a PrintStream, but that doesn't necessarily mean that its runtime concrete type actually is a PrintStream. It could just as easily be a subclass of PrintStream. Why isn't System.out declared as an abstract type? Simply because it was decided that it should offer the methods available in PrintStream, and PrintStream doesn't need anything else to be concrete.

As InputStream is abstract, we know that System.in is definitely a subclass. Why isn't it declared as the concrete class that it is? Because that could make it inflexible. Maybe under some circumstances (e.g. STDIN redirection) it might be a FileInputStream, and under other circumstances it could be another type of InputStream. Most likely, it's a concrete class whose definition is private.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42