Overloading
Learning Outcomes
- Method overloading
- constructors and ordinary methods
- Add a new class called Tree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | package com.ait.wk5;
public class Tree {
private String type;
public Tree() {
type = "Beech";
}
public Tree(String type) {
this.type = type;
}
public String getType() {
return type;
}
public String getType(String prefix) {
return prefix + " " + type;
}
}
|
- Write class called Overloading.java and run it.
1
2
3
4
5
6
7
8
9
10
11
12 | package com.ait.wk5;
public class Overloading {
public static void main(String[] args) {
Tree beech = new Tree(); // default constructor
System.out.println(beech.getType());
Tree oak = new Tree("Oak");
System.out.println(oak.getType());
System.out.println(beech.getType("The tree type is "));
}
}
|
Beech
Oak
The tree type is Beech
Tree.java
Overloading.java