2015-09-07 11:44:36 -06:00
|
|
|
// housekeeping/Overloading.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Both constructor and ordinary method overloading
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
class Tree {
|
|
|
|
int height;
|
|
|
|
Tree() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Planting a seedling");
|
2015-06-15 17:47:35 -07:00
|
|
|
height = 0;
|
|
|
|
}
|
|
|
|
Tree(int initialHeight) {
|
|
|
|
height = initialHeight;
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Creating new Tree that is " +
|
2015-06-15 17:47:35 -07:00
|
|
|
height + " feet tall");
|
|
|
|
}
|
|
|
|
void info() {
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"Tree is " + height + " feet tall");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
void info(String s) {
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
s + ": Tree is " + height + " feet tall");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Overloading {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
|
|
Tree t = new Tree(i);
|
|
|
|
t.info();
|
|
|
|
t.info("overloaded method");
|
|
|
|
}
|
|
|
|
// Overloaded constructor:
|
|
|
|
new Tree();
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Creating new Tree that is 0 feet tall
|
|
|
|
Tree is 0 feet tall
|
|
|
|
overloaded method: Tree is 0 feet tall
|
|
|
|
Creating new Tree that is 1 feet tall
|
|
|
|
Tree is 1 feet tall
|
|
|
|
overloaded method: Tree is 1 feet tall
|
|
|
|
Creating new Tree that is 2 feet tall
|
|
|
|
Tree is 2 feet tall
|
|
|
|
overloaded method: Tree is 2 feet tall
|
|
|
|
Creating new Tree that is 3 feet tall
|
|
|
|
Tree is 3 feet tall
|
|
|
|
overloaded method: Tree is 3 feet tall
|
|
|
|
Creating new Tree that is 4 feet tall
|
|
|
|
Tree is 4 feet tall
|
|
|
|
overloaded method: Tree is 4 feet tall
|
|
|
|
Planting a seedling
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|