Is this polymorphism?

10

Is this polymorphism? If so, why? I think it's because there is no method call. Please explain if I am correct or correct me. Thank you :)

OutputStream saida = new FileOutputStream("alunos.pdf");
    
asked by anonymous 26.07.2016 / 05:54

2 answers

14

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

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

OutputStream has the method write(byte[]) , which allows you to write sets of bytes somewhere. There are other similar methods as well. But in what place do these bytes be written? The answer is that it depends on the OutputStream implementation. In the case of FileOutputStream , they are written to a file. In the case of ByteArrayOutputStream , they are written to a buffer 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. Anyway, OutputStream provides all these different ways of writing bytes somewhere, a unified and standardized treatment.

In this way, OutputStream represents an abstraction , that is, a structure that allows you to encapsulate and hide the real complexity of performing operations. To say that it is hiding something may at first sight sound rather 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 on an internet connection, or where it would be very annoying. For example, imagine what the following code would look like if you had to worry about all of this:

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

Note that this 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 through the internet, etc. However, the purpose of this method is simply to write the string "Polimorfismo é legal" somewhere. The complexity of doing each of these forms of writing is encapsulated and hidden within OutputStream , in which case the programmer only cares about using the functionality offered while being spared the need to worry about the internal details of its functioning. That is, this complexity is abstracted .

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

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

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

    
26.07.2016 / 12:34
2

I know little about the OutputStream class, but FileOutputStream is a child class in which it extends from OutputStream as an inheritance, so daughters can use certain attributes and override their methods if you need to redefine some behaviors in this inheritance. From what I learned in the past simesis and that when a mother class can receive any stay of their daughters is considered polymorphism, by the definition of polymorphism, is the state of being able to assume different forms, is the way that an object can be referenced from various forms.

Ex. A manager is an employee, soon manager would then extend that would be his parent class, which in turn besides officials would have his specialties, then:

Official fGer = new Manager ();

    
26.07.2016 / 12:47