Inheritance, Polymorphism, and Interfaces
Inheritance, Polymorphism, and Interfaces
Polymorphism
• Polymorphism is the ability of an object to take on many forms.
• Polymorphism allows you to make changes in the method definition for the derived classes and have
those changes apply to the methods written in the base class.
• The most common use of polymorphism occurs when a base class reference is used to refer to a
derived class object.
• When an overridden method is invoked, its action is the one defined in the class used to create the
object using the new operator. It is not determined by the type of the variable naming the object.
• Dynamic binding allows many meanings to be associated to one method name.
• In dynamic binding, the definition of a method is not bound to an invocation of the method until run
time when the method is called. This is the mechanism used by polymorphism.
• Example of polymorphism: (Assume that Undergraduate inherits from the class Student.)
public class PolymorphismSample {
public static void main (String[] args) {
Person[] people = new Person[4];
people[0] = new Undergraduate(“Velasquez, Veronica”, 4910, 1);
people[1] = new Undergraduate(“Santos, Stephanie”, 9931, 2);
people[2] = new Student(“Mendoza, Marie”, 8812);
References:
Baesens, B., Backiel, A. & Broucke, S. (2015). Beginning java programming: The object-oriented approach.
Indiana: John Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th Edition. Boston: Course Technology, Cengage Learning
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th Edition. California: Pearson
Education, Inc.