Polimorfismo em Java

Na unidade 6 do curso de Programação 1 aprendemos o conceito de polimorfismo, termo derivado do grego poli — muitos —, e morpheus — forma, estrutura. Uma das tarefas é discutir polimorfismo no contexto de programação orientada a objetos e dar um exemplo de como implementar essa funcionalidade em Java.

Revisando as anotações achei que a minha abordagem foi interessante. Segue para a apreciação, com pequenas modificações.


The world around us is polymorphic. We can see this almost everywhere: people, animals, cars, books, languages, houses. As humans, we seek to differentiate ourselves from others. We like to think and show that we are unique up to a point in which other people still recognize us as humans. Up to a point that they still can relate and interact with us.

Something similar happens with a programming language like Java. The ability to instantiate and handle multiple objects together make complex programs easier to handle, manage and develop. It is expected that complex software keeps evolving and including new functionalities with time. Imagine that if for each new functionality you have, you need to review previous objects and update old code to fit each new features included. It can easily become a complex and time-consuming task. Polymorphism is a powerful solution that avoids a lot of code rework and guarantee program fluidity. Citing Oracle Java documentation definition, “subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.” (Oracle, 2017)

Imagine the following Language superclass and English subclass.

class Language {
    void toHear(Language hear) {
    // Method to hear some language
}
toSpeak(Language speak) {
    // method to speak some language
    }
}

class English extends Language {
	void toHear(English hear) {
	// specific instructions to hear and understand the English language.
	}
	toSpeak(English speak) {
	// specific instructions to hear and understand the English language.
	}
}

Imagine that in the example above you have multiple language subclasses and want then all to “work” in their own language. Of course that this is a simplified example, but the same method toSpeak would call a specific response for each object (aka each language), a unique behavior, while still sharing some functionality from the parent class.

Reference: Oracle (Ed.). (n.d.). Polymorphism. Retrieved October 18, 2017, from https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Leave a Reply

Your email address will not be published. Required fields are marked *