- Hands-On Design Patterns with Java
- Dr. Edward Lavieri
- 443字
- 2021-06-24 14:57:59
Overloading constructors and methods
When constructors and methods are declared, we include the expected parameters. For example, our Bicycle() constructor does not take any parameters. We can overload that constructor by creating one or more versions of it, each with a different set of parameters. Let's look at our Bicycle class with four constructors:
// constructor - default
Bicycle() {
}
// constructor - String parameter
Bicycle(String aColor) {
this.color = aColor;
}
// constructor - int parameter
Bicycle(int nbrOfGears) {
this.gears = nbrOfGears;
}
// constructor - int, double, double, String parameters
Bicycle(int nbrOfGears, double theCost, double theWeight, String aColor) {
this.gears = nbrOfGears;
this.cost = theCost;
this.weight = theWeight;
this.color = aColor;
}
The default constructor is used when an instance of the object is created without any parameters. We would use the following code link:
Bicycle myBike1 = new Bicycle();
We can use one of the overloaded methods when creating a Bicycle object, simply by passing the proper arguments, based on what the overloaded method expects as parameters. The following Driver class shows the creation of four Bicycle objects, one using a different constructor:
public class Driver {
public static void main(String[] args) {
Bicycle myBike1 = new Bicycle();
Bicycle myBike2 = new Bicycle("Brown");
Bicycle myBike3 = new Bicycle(22);
Bicycle myBike4 = new Bicycle(22, 319.99, 13.5, "White");
myBike1.outputData();
myBike2.outputData();
myBike3.outputData();
myBike4.outputData();
}
}
As you can see from the following output provided, each Bicycle object was created with a different constructor. Overloading the constructor increases the flexibility in object creation:

We can also overload methods. For example, we have used the outputData() method in our Bicycle class several times already. Let's overload that method so that we can pass additional text to be printed in the output. Here are both versions of that method:
// method to output Bicycle's information
public void outputData() {
System.out.println("\nBicycle Details:");
System.out.println("Gears : " + this.gears);
System.out.println("Cost : " + this.cost);
System.out.println("Weight : " + this.weight + " lbs");
System.out.println("Color : " + this.color);
}
// method to output Bicycle's information - overloaded
public void outputData(String bikeText) {
System.out.println("\nBicycle " + bikeText + " Details:");
System.out.println("Gears : " + this.gears);
System.out.println("Cost : " + this.cost);
System.out.println("Weight : " + this.weight + " lbs");
System.out.println("Color : " + this.color);
}
As you can see in the preceding code, our overloaded method accepts a String parameter and incorporates that in the first printed line. We can modify our Driver class as shown in the following code snippet:
myBike1.outputData("Nbr 1");
myBike2.outputData("Nbr 2");
myBike3.outputData("Nbr 3");
myBike4.outputData("Nbr 4");
The output is presented here and illustrates how easy it is to create and use overloaded methods:

- 企業大數據系統構建實戰:技術、架構、實施與應用
- Access 2016數據庫技術及應用
- 數據革命:大數據價值實現方法、技術與案例
- 智能數據時代:企業大數據戰略與實戰
- AI時代的數據價值創造:從數據底座到大模型應用落地
- 云原生數據中臺:架構、方法論與實踐
- Hadoop 3實戰指南
- Web Services Testing with soapUI
- Mastering ROS for Robotics Programming(Second Edition)
- 數據庫查詢優化器的藝術:原理解析與SQL性能優化
- MySQL數據庫應用與管理
- 數據挖掘與機器學習-WEKA應用技術與實踐(第二版)
- 大數據隱私保護技術與治理機制研究
- 數據庫基礎與應用
- 一本書講透數據治理:戰略、方法、工具與實踐