Override
A subclass can define a method that overrides a method in the superclass.
- The subclass can define new behavior for this method.
The subclass method must have exactly the same signature as the superclass method.
public class Person {
// ...
public String getInfo() {
return firstName + " " + lastName + " " + age;
}
}
public class Employee extends Person {
// ...
// OVERRIDE - exact same signature as superclass
public String getInfo() {
return super.getInfo() + " " + title + " " + salary;
}
}
method signature¶
A method's name, and the number, order, and types of its parameters.
Parameter names are not part of the signature. The method return type is not part of the signature, though overriding imposes some rules on return types.
If we have a Person reference to an Employee object, Employee's getInfo() method will be called.
Person pers = new Employee(); // pers --> Employee object
pers.getInfo(); // Employee's getInfo()
If we have an Employee reference to a DataAnalyst or SoftwareDeveloper object, each class's getInfo() method is called.
public class DataAnalyst extends Employee {
// ...
public String getInfo() {
return super.getInfo() + " " + securityClearance;
}
}
public class SoftwareDeveloper extends Employee {
// ...
public String getInfo() {
return super.getInfo();
}
}
Employee empSoftware = new SoftwareDeveloper();
Employee empData = new DataAnalyst();
empSoftware.getInfo(); // calls SoftwareDeveloper method
empData.getInfo(); // calls DataAnalyst method

If the object does not have its own implementation of a method, its inherited method will be called.
The DatabaseAdmin class does not have its own getInfo() method, so Employee's method is called.
Employee empSoftware = new SoftwareDeveloper();
Employee empData = new DatabaseAdmin();
empSoftware.getInfo(); // calls SoftwareDeveloper method
empData.getInfo(); // calls Employee method

The process of method selection at runtime is known as dynamic binding.
dynamic binding¶
Determining at runtime which method to call.
Java bases the method to call on the type of object in memory instead of the type of the reference.
Practice Exercise¶
Remember: the method that is chosen to run is based on the object type in memory, not the reference variable's type. This is a common interview and exam question, and is one of the key features of polymorphism.
When there is an override...
Method Choice = MEMORY
Drill¶
You will be adding a public void executeJob(String data) method to each class, and running an application to see each class's custom behavior.
Polymorphism/com.example.polymorphism.drills.employee.Employee
- Add a method
public void executeJob(String data)which prints the messageExecuting job+ data.- Other classes will override this method.
Polymorphism/com.example.polymorphism.drills.employee.DataAnalyst* Add a methodpublic void executeJob(String data)which callsanalyzeData(String)and passes thedata. * Print the returnedStringto the screen.
Polymorphism/com.example.polymorphism.drills.employee.DatabaseAdmin
* Add a method public void executeJob(String data) which calls createTables(String) and passes the data.
* Print the returned String to the screen.
Polymorphism/com.example.polymorphism.drills.employee.SoftwareDeveloper
* Add a method public void executeJob(String data) which calls produceSoftware(). (This does not do anything with data.)
* Print the returned String to the screen.
Polymorphism/com.example.polymorphism.drills.employee.ExecuteJobApp
- Uncomment the calls to
executeJob.- Run the class to see
getInfoandexecuteJobexecuted for each class.- Remember that only
DataAnalysthas an overridinggetInfomethod that changes behavior.SoftwareDeveloperhas an overridinggetInfomethod, but it only calls its parent class'sgetInfomethod.- Optional: refactor this class to reduce the copy-pasted code in
run().- Optional: place breakpoints in each class's
getInfoandexecuteJobmethods, and Debug As to trace method execution.(Solution: ExecuteJobApp.java, Employee2.java, DataAnalyst2.java, DatabaseAdmin2.java, SoftwareDeveloper2.java)