Re: Polymorphism in Java SE?
Stefan Ram wrote:
Patricia Shanahan <pats@acm.org> writes:
A near miss is the field ?System.in?, which indeed contains an
object of a proper /subtype/ of the field's type. So it is
somewhat polymorphic. You do not know the actual type until
run-time. It is only boring insofar as it is always the same
subtype.
Huh? Why do you say it is always the same subtype?
Said ?always the same subtype?,
thought ?always the same subtype at the start of a program?.
Sorry, I should also have written ?at the start of a program?.
System.setIn(someStream);
Yes, indeed. This is possible. Thank you.
Here one party (your code) sets the field and then /the same
party/ reads it in again. I believe it might be more
suggestive when polymorphism is used for communication between
/two/ parties. Therefore, I searched for a Java-SE factory
method, so that I can show, how Java SE conveys information to
an application by a polymorphic expression.
Indeed. The program I posted was just intended to demonstrate some of
the System.in possibilities.
To use this to demonstrate polymorphism, I would write some simple
method that reads from System.In and uses the data to do its job. I
would then write two main programs to control it, one using standard
input and the other forcing a different data source.
This approach can be useful, for example, for testing a class that
normally reads from standard input. The test code can force its own data
using System.setIn.
Patricia