JAVA: What is the difference between OutputStream and getOutputStream ()?

6

I'm studying java and I came across various input and output classes, but I had a question about the difference between OutputStream and getOutputStream() .

As far as I understand, getOutputStream() takes what you're going through. And the OutputStream ?

    
asked by anonymous 10.10.2017 / 20:57

2 answers

10

OutputStream is a class strong>. It corresponds to an object that writes byte sequences somewhere.

There are several other objects that have a method named getOutputStream() which, as the name indicates, returns an object of type OutputStream (or some of its subclasses).

Anyway, getOutputStream() is the method you call to get an instance of a OutputStream to write something somewhere.

The place where writing will occur depends on the instance of OutputStream , and therefore depends on the object that has the getOutputStream() method.

    
10.10.2017 / 21:07
7

In addition to Victor's response, which correctly emphasizes the difference between the type (class OutputStream ) and the object access method of that type ( getOutputStream ), I find it important to point out that ...

1. Methods with the same name can return other types of objects

The ServletResponse#getOutputStream method, for example, returns a ServletOutputStream , which is still a subclass of OutputStream , but it's still important to know that it's a different concrete type.

2. Methods of the same name exist in different classes with different purposes

Examples:

3. Java has a complex hierarchy of stream classes for input and output

It is not necessary to be an expert on each of them, but for each situation involving streams of data it is important to consult the documentation and understand the best type to work according to the features offered .

    
10.10.2017 / 21:37