Please enable JavaScript to use CodeHS

CodeHS Glossary


Polymorphism Java

Polymorphism is the capability of a method to do different things depending on which object it is acting upon. Subclasses can have their own behavior *in addition* to the behavior of the superclass. If a method is defined on both the superclass and the subclass, the proper version of the method will be called depending on the type of the object. Suppose Rectangle and Circle both extend the class Shape: ``` Shape rect = new Rectangle(10, 20); Shape circ = new Circle(30); // Rectangle's version of getArea will be called double area1 = rect.getArea(); // Circle's version of getArea will be called double area2 = circ.getArea(); ```