-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_OverloadedConstructors
More file actions
49 lines (41 loc) · 1.24 KB
/
29_OverloadedConstructors
File metadata and controls
49 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// overloaded constructors = multiple constructors within a class with the same name,
but have different parameters
name + parameters = signature
<Main.java>
public class Main {
public static void main(String[] args){
Pizza pizza = new Pizza("thick crust","tomato");
System.out.println("Here are the ingredients of your pizza: ");
System.out.println(pizza.bread);
System.out.println(pizza.sauce);
System.out.println(pizza.cheese);
System.out.println(pizza.topping);
}
}
<Pizza.java>
public class Pizza {
String bread;
String sauce;
String cheese;
String topping;
Pizza(String bread){
this.bread = bread;}
Pizza(String bread, String sauce){
this.bread = bread;
this.sauce = sauce;}
Pizza(String bread, String sauce, String cheese){
this.bread = bread;
this.sauce = sauce;
this.cheese = cheese;}
Pizza(String bread, String sauce, String cheese, String topping){
this.bread = bread;
this.sauce = sauce;
this.cheese = cheese;
this.topping = topping;}
}
>>
Here are the ingredients of your pizza:
thick crust
tomato
null
null