Is that a polymorphism?

Asked

Viewed 353 times

11

Is that polymorphism? If so, why? I think it’s because there’s no method call. Please explain to me if I’m correct or correct me. Thank you :)

OutputStream saida = new FileOutputStream("alunos.pdf");

2 answers

15


Yes, this is inclusion polymorphism (also called subtype polymorphism). Note that the OutputStream can be implemented concretely by several different classes, such as the FileOutputStream or the ByteArrayOutputStream, among others.

In Java, this form of polymorphism is commonly implemented through interfaces or by inheritance between classes (there are other less orthodox and more complex forms as well, but as the subject here is something introductory, let’s leave the rest aside). In the case of OutputStream, which is an abstract class, the subclasses complement, complement, refine and/or replace superclass behavior. Something similar occurs with interfaces as well, especially from Java 8 where interfaces can have methods with implementation default.

The OutputStream has the method write(byte[]), that allows you to write byte sets somewhere. There are other similar methods as well. But, where are these bytes written? The answer is that it depends on the implementation of OutputStream. In the case of FileOutputStream, they are written in a file. In the case of ByteArrayOutputStream, they are written in a buffer from memory. Other existing implementations allow written bytes to be sent over the network or over the Internet or directed to peripheral devices to be interpreted as commands, among many other possibilities. Finally, the OutputStream provides all these different ways to write bytes somewhere, a unified and standardized treatment.

Thus, the OutputStream represents a abstraction, that is, a structure that allows to encapsulate and hide the real complexity of performing operations. Saying that he is hiding something may at first sound a little strange, but when you want to write bytes somewhere, having to worry about whether it is in a file, whether it is in memory, whether it is in an internet connection, or where it is would be very boring. For example, imagine what the code below would look like if you had to worry about all this:

public void escreveTexto(OutputStream stream) {
    byte[] escrever = "Polimorfismo é legal".toByteArray();
    stream.write(escrever);
}

Note that this above code would be horribly more complicated if we had to have a version that writes this in memory, one that writes to the file, one that sends it over the internet, etc. However, the purpose of this method is simply to write the string "Polimorfismo é legal" somewhere. The complexity of making each of these forms of writing gets encapsulated and hidden inside the OutputStream, and in this case the programmer only cares to use the functionality offered being spared the need to worry about the internal details of its operation. I mean, this complexity is abstracted.

Well, and then we realized that the OutputStream may exist on many forms different. There are several implementations for it as mentioned, each doing the process of writing bytes somewhere in a way. In Greek, "poly" means "many" and "morphs" means "form", and that’s what polymorphism is, something that has many different forms. OutputStream is polymeric because it can be implemented in many different ways.

There are also other forms of polymorphism besides inclusion polymorphism. Overload is a form of polymorphism, which in Java appears when in the same class you have methods with the same name, but parameters of different types. Another form of polymorphism is coercion, which allows to convert int in long or float in double without having to make a cast explicit. We still have parametric polymorphism, which is provided in Java through Generics.

Polymorphism can also be classified as static or dynamic. The static is one that can be solved at compile time (coercion and overload), while the dynamic requires information available only at run time (inclusion polymorphism and parametric).

  • 2

    I want to use Bookmark in this response. Very well!!!

2

I know little about the Outputstream class, but Fileoutputstream is a daughter class in which you extend Outputstream as an inheritance, so the daughter classes can use certain attributes and overwrite their methods if you need to redefine some behaviors in this inheritance. From what I’ve learned in the past symmetry and that when a mother class can receive any resistance from its daughters it’s considered polymorphism, by the definition of polymorphism, it’s the state of being able to take different forms, is the way an object can be referenced in various ways.

Ex. A manager is an employee, then manager would extend employee who would be his mother class, which in turn would not only employees would have their specialties, so:

Staff fGer = new Manager();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.