- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 954字
- 2021-06-11 13:05:22
Overriding and Hiding Methods
When extending a class, it is possible to redefine some of the methods that are part of it. Overriding means to rewrite something's functionality. This is done by making a new declaration of the method with the same name and properties of the method from the original class. This is demonstrated in the following example. Note that we're continuing, for the sake of clarity, with Computer and Tablet, but they have been cleaned up so as not to make the example programs too long.
class Computer {
public void whatIsIt() {
System.out.println( "it is a PC");
}
}
class Tablet extends Computer {
public void whatIsIt() {
System.out.println( "it is a tablet");
}
}
class Example06 {
public static void main(String[] args) {
Tablet myTab = new Tablet();
myTab.whatIsIt();
}
}
Since Tablet extends Computer, you could modify the main class in the program to be as follows:
class Example06 {
public static void main(String[] args) {
Computer myTab = new Tablet();
myTab.whatIsIt();
}
}
Technically, tablets are computers, which means that you can create an object of the Tablet class by defining it as Computer in the first place. The result for both cases will be the same:
it is a tablet
Process finished with exit code 0
The result is the same for both classes because both the child and parent classes include a non-static method called whatIsIt(). When calling the method, the overriding one will have priority. This is done by the JVM at runtime. This principle is what we call runtime polymorphism. There can be multiple definitions of the same method, and which definition will be executed is decided during the execution of the program.
But what would happen if the method you called was static? This could be a design decision taken by the developer who is creating the class you are extending and therefore is a situation out of your control. In this case, it is not possible to override the method. The child class can, however, hide the method defined by the parent using the same mechanism. The next code listing demonstrates this.
class Computer {
public static void whatIsIt() {
System.out.println( "it is a PC");
}
}
class Tablet extends Computer {
public static void whatIsIt() {
System.out.println( "it is a tablet");
}
}
class Example07 {
public static void main(String[] args) {
Computer myTab = new Tablet();
myTab.whatIsIt();
}
}
The outcome of this example is:
it is a PC
Process finished with exit code 0
The decision of what method should be used with static methods is not taken at runtime but during compilation, and this ensures that the method from the parent class is the one being called. This action is called hiding instead of overriding. It is still possible to call the method in the Tablet class. To do so, you should modify the code in the main class to the following:
class Example07 {
public static void main(String[] args) {
Computer myTab = new Tablet();
Tablet.whatIsIt();
}
}
Note how we clearly specify the actual class you call for this. The result of the modified example is:
it is a tablet
Process finished with exit code 0
Avoiding Overriding: Final Classes and Methods
If you want to stop other developers from overriding parts of your class, you can declare the methods you want to protect as final. An example of this could be a class that deals with temperature. The method that converts from Celsius into Fahrenheit is final, as it makes no sense to override such a method.
class Temperature {
public double t = 25;
public double getCelsius() {
return t;
}
final public double getFahrenheit() {
return t * 9/5 + 32;
}
}
class Example08 {
public static void main(String[] args) {
Temperature temp = new Temperature();
System.out.println( temp.getCelsius() );
System.out.println( temp.getFahrenheit() );
}
}
This program will give this result:
25.0
77.0
Process finished with exit code 0
Note
Alternatively, you can declare a whole class final. A final class cannot be extended. An example of such a class is String. You could ask whether it defeats the purpose of object-oriented programming to have a class that cannot be extended. But there are some classes that are so fundamental to the programming language, such as String, that they are better kept as they are.
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- 圖解Java數據結構與算法(微課視頻版)
- MySQL 8 DBA基礎教程
- BeagleBone Media Center
- Neo4j Essentials
- PostgreSQL Replication(Second Edition)
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- Java網絡編程實戰
- Odoo 10 Implementation Cookbook
- 零基礎學C++(升級版)
- 關系數據庫與SQL Server 2012(第3版)
- Joomla!Search Engine Optimization
- VMware vSphere 5.5 Cookbook
- Elastix Unified Communications Server Cookbook
- 跟小樓老師學用Axure RP 9:玩轉產品原型設計