17
When and why should we use polymorphism in Java, because so far I’ve only used it to make multiple windows based on a model.
The polymorphism the way I’m applying is better than making one window independent of the other?
17
When and why should we use polymorphism in Java, because so far I’ve only used it to make multiple windows based on a model.
The polymorphism the way I’m applying is better than making one window independent of the other?
19
Polymorphism is when using inheritance, interface or abstract class, so a class is more than one "type" at the same time.
Example: If you have a "Dog" class that is inherited from the "Animal" class, the instances of the "Dog" class will be both a Dog and an Animal.
This is not usually so applied to data modeling, but can be used in to solve software.
Example: You have a program where you can save/export data in both XML and ZIP, at the user’s choice. In this case "XML" and "ZIP" can be classes that sign an interface "File" that says they have to have a method "read" and another "save". When you call the "save" method, you only know that it is a File, but you do not know if it is XML or ZIP type.
Something similar can be used if you want your program to work on both Mysql and Postgres, for example.
Anyway, it also goes from your creativity.
So it’s all about preference and need?
@gcarvalho97 Yes, your needs will indicate when it is appropriate to use it.
What was said here is explanation of inheritance and not of polymorphism. Much less answers what was asked. I did not understand the acceptance (it is even a direct from the AP), even less so many votes in a response that is basically wrong (at least in this context).
"Go from your creativity"?!?! If you are programming in OO, you are probably using polymorphism without knowing. Briefly, it is the mechanism that allows class heirs to overwrite implementations of inherited classes. The example about interfaces and classes, relationship with heritages is OK, but the concepts are two different things. There is no room for "creativity" in the definition of polymorphism.
17
I will answer what you give, because the answer that exists does not explain what was actually asked. Can’t tell if you’re doing something right without seeing what you’re doing.
What you apparently are doing with the windows is just inheritance. That generates a subclass and a subtype. Polymorphism may occur together, but nothing indicates that this occurs in the example.
In Java the main way to use polymorphism is through inheritance.
Polymorphism is the substitution of one type for another that fits with what is intended to be done. Through inheritance it is guaranteed that the suitability occurs, at least technically, can not be conceptually guaranteed.
So if you have a class that’s a window model and the concrete windows are inherited from it, there’s inheritance and probably polymorphism in various situations. But it is important to note that they are different mechanisms. So much so that polymorphism can occur in other ways and inheritance does not require being polymorphic.
Some people think that polymorphism is something tied to OOP. It’s even in one direction, no OOP without polymorphism, but there is polymorphism without OOP.
To understand polymorphism think of a method you wrote that receives JanelaModelo
and inside that object do some things, for example call adicionarWidget()
.
Consider that for some reason at JanelaPrincipal
needed to change the implementation. When the implementation is not changed the polymorphism does not matter.
Now you will call this method, but you will pass an object like JanelaPrincipal
, which you obviously inherited from JanelaModelo
. As there is inheritance this will be accepted, all that this method can use in one type is certainly present in the other, given the inheritance.
What method adicionarWidget()
will be called? The existing in JanelaModelo
or existing in JanelaPrincipal
?
If polymorphism did not exist, it will be called the JanelaModelo
and will add widgets where it should not. With polymorphism there is a substitution and who will be called is the method of JanelaPrincipal
, even though originally this method was thought to work with JanelaModelo
. So everything will work as expected.
In practice, in those cases described in the example, when you have an inheritance and overlap of a method in the derived class.
Note that Java does polymorphism by default. Unless you declare a method like final
it is virtual and therefore the polymorphism (substitution) will occur whenever there is an equal method in the derived class. So in a way you’re using it without even realizing it. Maybe that’s why so many people confuse inheritance with polymorphism.
When creating an interface the polymorphism will always occur. And it is desirable to use interfaces whenever possible.
In this case a method (or other situation expected to be an object type) is created that will receive the interface. Since interfaces are abstract, an object based on it will never be passed, only an object that will be subtype of it, that is, that implements something determined in it. And the method in the class that implements it will certainly have an implementation, whereas the interface will certainly not have, there is superscript. So polymorphism will certainly occur.
Java 8 has interfaces that subclass, is a little different, but let’s not complicate.
There are often abuses of the use of polymorphism, especially in Java. It is not always possible to do polymorphism, or even inheritance, and everything works as expected. There is even the the principle of Liskov’s substitution that deals with that subject.
In your windows you’re using polymorphism and not even realizing it. You’re not doing it with intent.
A real example on the subject.
Another good practical example.
To facilitate generalization of algorithms and data structures.
Imagine that without it you would have to have a method that you receive JanelaPrincipal
, another method that does exactly the same thing and changes only that it receives JanelaCliente
, another to JanelaProduto
, and so on. It would be absurd. Polymorphism allows you to use only one method that you turn to choose the correct code to use.
Some languages do this differently, one of them is the compiler identify the type difference and generate a new version of the code used specific to that type. This has advantages and disadvantages.
Java uses a indirect and has only one unique code. That is, when you need to call a polymorphic method instead of making the direct call, it looks in a virtual table (vtable) who should be called. When you pass an object of a type, the table linked to that type is accessed. Because of this indirect (it has two steps to call the right method) there is an almost insignificant loss of performance.
More details can be seen on Polymorphism in procedural language.
To understand how it can function non-colymously (C#).
Browser other questions tagged java oop software-engineering polymorphism
You are not signed in. Login or sign up in order to post.
Some Links that Can Help You : 1. Caelum Workbook on Object Orientation 2. Polymorphism - Post Blog Javaprogressive
– user13219
Related: In object orientation, why interfaces are useful? and Why create an object using the superclass?
– Math
I gave an answer that I consider more appropriate to the question. @g.carvalho97 take a look and say what you think.
– Maniero