1
Connection connection;
connection = new ConnectionFactory().getConnection();
I can say that doing this is polymorphism?
The Connection
receive the class connection ConnectionFactory
.
1
Connection connection;
connection = new ConnectionFactory().getConnection();
I can say that doing this is polymorphism?
The Connection
receive the class connection ConnectionFactory
.
9
Yes.
The type of return of the method getConnection
is the interface Connection
. Soon, you are seeing only the interface to not the implementation, which can be of various "flavors".
The initial Wikipedia definition for polymorphism is a little misleading in defining the term as "references of more abstract class types represent the behavior of concrete classes".
This may imply that polymorphism only exists when there is inheritance, what is not true.
However, reading further it is clear that this was only a simplification, as the text goes on to say that with polymorphism "it is possible to treat several types in a homogeneous way (through the interface of the most abstract type)" and that "one of the ways to implement polymorphism is through an abstract class,".
Finally, there are several types of polymorphism, one of which uses mechanisms of extension or inheritance. It is worth remembering that in some languages there can be multiple inheritance, that is, inherit several classes, while in Java this is done using interfaces. As for polymorphism, the result of both techniques is practically the same.
The vast majority of real (non-academic) applications use some different type of Connection
.
The simplest example is when you use a pool of connections, because the close
does not actually close the connection, but returns to the pool of available connections.
This is done by implementing a Connection
different that encapsulates the true connection, that is, another class that also implements Connection
.
The entire API of JDBC
is made using polymorphism so that you are always using abstract types or interfaces and never directly deploying them. This makes your code work well with all the different types of databases supplying all the different implementations (except of course the SQL code).
If the JDBC API was not implemented with polymorphism, we would have to instantiate and reference specific classes for each different type of database or library.
Examples:
MySqlConnection con1 = mySqlConnectionFactory.getConnection();
MySqlStatement st1 = con1.createStatement("select * from tabela");
MySqlResultSet rs1 = st1.executeQuery();
OracleConnection con2 = oracleConnectionFactory.getConnection();
OracleStatement st2 = con2.createStatement("select * from tabela");
OracleResultSet rs2 = st2.executeQuery();
WHAT? So I’m right? But someone else treated me with arrogance and told me that nay This is not polymorphism... : /
@Aline What may have caused difficulties with the other answer was the other piece of code you posted before editing.
It certainly wasn’t that; the other person said that if there is no inheritance there is no polymorphism. He is level beginner tbm...
But thank you for the reply @utluiz
Browser other questions tagged java polymorphism
You are not signed in. Login or sign up in order to post.
Look without looking at the implementation not to say more is more to inheritance
– Al Unser Albuquerque
http://answall.com/q/89309/101 http://answall.com/q/46724/101 http://answall.com/q/80948/101 http://answall.com/q/25100/101 http://answall.com/q/53108/101
– Maniero
@bigown then study his answer. : D <3
– Aline
@Alinegonzaga He removed his answer.
– utluiz