-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathFlexibleConstructorBodiesExample.java
56 lines (46 loc) · 1.41 KB
/
FlexibleConstructorBodiesExample.java
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
50
51
52
53
54
55
56
/*
* To run: `java --enable-preview --source 23 FlexibleConstructorBodiesExample.java`
*/
public class FlexibleConstructorBodiesExample {
public static void main(String[] args) {
System.out.println("Pet:\n" + new Dog("Shih Tzu"));
}
}
abstract class Animal {
protected final String domain;
protected final String kingdom;
protected final String phylum;
protected final String subphylum;
protected final String phylumClass;
protected final String family;
private final String representation;
protected Animal(String phylum, String subphylum, String phylumClass, String family) {
this.domain = "Eukaryota";
this.kingdom = "Animalia";
this.phylum = phylum;
this.subphylum = subphylum;
this.phylumClass = phylumClass;
this.family = family;
this.representation = getClassification();
}
public abstract String getClassification();
public String toString() {
return this.representation;
}
}
class Dog extends Animal {
private final String order;
private final String species;
private final String breed;
Dog(String breed) {
this.order = "Carnivora";
this.species = "Dog";
this.breed = breed;
super("Chordata", "Vertebrata", "Mammals", "Canidae");
}
public String getClassification() {
return "Domain: %s / Kingdom: %s / Phylum: %s / Subphylum: %s / Class: %s / Order: %s/ Species: %s / Breed: %s".formatted(
domain, kingdom, phylum, subphylum, phylumClass, order, species, breed
);
}
}