6. Classes
6.1. Basic
1public static class Car {
2 String make;
3 String model;
4 int year;
5
6 public Car(String make, String model, int year) {
7 this.make = make;
8 this.model = model;
9 this.year = year;
10 }
11}
1var car = new Car("Honda", "Accord", 2019);
2
3System.out.println(car.make + " " + car.model + " " + car.year);
6.2. Getters and setters
1public static class Car {
2 private String make;
3 private String model;
4 private int year;
5
6 public Car(String make, String model, int year) {
7 this.make = make;
8 this.model = model;
9 this.year = year;
10 }
11
12 public String getMake() {
13 return make;
14 }
15
16 public String getModel() {
17 return model;
18 }
19
20 public int getYear() {
21 return year;
22 }
23
24 public void setMake(String make) {
25 this.make = make;
26 }
27
28 public void setModel(String model) {
29 this.model = model;
30 }
31
32 public void setYear(int year) {
33 this.year = year;
34 }
35}
1var car = new Car("Honda", "Accord", 2019);
2
3System.out.println(car.getMake() + " " + car.getModel() + " " + car.getYear());
6.3. Method overriding
1public static class Car {
2 private String make;
3 private String model;
4 private int year;
5
6 public Car() {
7 this.make = "None";
8 this.model = "None";
9 this.year = -1;
10 }
11
12 public Car(String make, String model, int year) {
13 this.make = make;
14 this.model = model;
15 this.year = year;
16 }
17
18 public String getMake() {
19 return make;
20 }
21
22 public String getModel() {
23 return model;
24 }
25
26 public int getYear() {
27 return year;
28 }
29
30 public void setMake(String make) {
31 this.make = make;
32 }
33
34 public void setModel(String model) {
35 this.model = model;
36 }
37
38 public void setYear(int year) {
39 this.year = year;
40 }
41
42 @Override
43 public String toString() {
44 return make + " " + model + " " + year;
45 }
46
47 @Override
48 public boolean equals(Object object) {
49 if (object == this) {
50 return true;
51 }
52
53 if (null == object || !(object instanceof Car)) {
54 return false;
55 }
56
57 var that = (Car)object;
58 if (this.make.equals(that.make)
59 && this.model.equals(that.model)
60 && this.year == that.year) {
61 return true;
62 }
63
64 return false;
65 }
66
67 @Override
68 public int hashCode() {
69 int result = 17;
70 result = 31 * result + make.hashCode();
71 result = 31 * result + model.hashCode();
72 result = 31 * result + (new Integer(year)).hashCode();
73 return result;
74 }
75}
1var car = new Car("Honda", "Accord", 2019);
2
3System.out.println(car);
4
5var car1 = new Car("Honda", "Accord", 2019);
6var car2 = new Car("Honda", "Accord", 2019);
7var car3 = new Car("Honda", "Accord", 2020);
8
9System.out.println(car1.equals(car2));
10System.out.println(car1.equals(car3));
11
12System.out.println(car1.hashCode());
13System.out.println(car2.hashCode());
14System.out.println(car3.hashCode());
6.4. Method with logic
1public static class Car {
2 private String make;
3 private String model;
4 private int year;
5 private double milesPerGallon;
6
7 public Car() {
8 this("None", "None", -1, 0.0d);
9 }
10
11 public Car(String make, String model, int year, double milesPerGallon) {
12 this.make = make;
13 this.model = model;
14 this.year = year;
15 this.milesPerGallon = milesPerGallon;
16 }
17
18 public String getMake() {
19 return make;
20 }
21
22 public String getModel() {
23 return model;
24 }
25
26 public int getYear() {
27 return year;
28 }
29
30 public double getMilesPerGallon() {
31 return milesPerGallon;
32 }
33
34 public double getMiles(double gallons) {
35 return milesPerGallon * gallons;
36 }
37
38 public double getGallons(double miles) {
39 return (1.0 / milesPerGallon) * miles;
40 }
41
42 @Override
43 public String toString() {
44 return make + " " + model + " " + year;
45 }
46
47 @Override
48 public boolean equals(Object object) {
49 if (object == this) {
50 return true;
51 }
52
53 if (null == object || !(object instanceof Car)) {
54 return false;
55 }
56
57 var that = (Car)object;
58 if (this.make.equals(that.make)
59 && this.model.equals(that.model)
60 && this.year == that.year
61 && this.milesPerGallon == that.milesPerGallon) {
62 return true;
63 }
64
65 return false;
66 }
67
68 @Override
69 public int hashCode() {
70 int result = 17;
71 result = 31 * result + make.hashCode();
72 result = 31 * result + model.hashCode();
73 result = 31 * result + (new Integer(year)).hashCode();
74 result = 31 * result + (new Double(milesPerGallon).hashCode());
75 return result;
76 }
77}
1Car car = new Car("Honda", "Accord", 2019, 22.0d);
2
3var miles = 100;
4var gallons = 20;
5
6var gallonsRequired = car.getGallons(miles);
7var mileage = car.getMiles(gallons);
8
9System.out.println(car + " can travel " + miles +
10 " but needs " + String.format("%.2f", gallonsRequired) + " gallons of gas");
11
12System.out.println(car + " with " + gallons +
13 " gallons of gas can travel " + mileage + " miles");
6.5. Static Method
1public static class Car {
2 private String make;
3 private String model;
4 private int year;
5 private double milesPerGallon;
6
7 public Car() {
8 this("None", "None", -1, 0.0d);
9 }
10
11 public Car(String make, String model, int year, double milesPerGallon) {
12 this.make = make;
13 this.model = model;
14 this.year = year;
15 this.milesPerGallon = milesPerGallon;
16 }
17
18 public String getMake() {
19 return make;
20 }
21
22 public String getModel() {
23 return model;
24 }
25
26 public int getYear() {
27 return year;
28 }
29
30 public double getMilesPerGallon() {
31 return milesPerGallon;
32 }
33
34 public double getMiles(double gallons) {
35 return milesPerGallon * gallons;
36 }
37
38 public double getGallons(double miles) {
39 return (1.0 / milesPerGallon) * miles;
40 }
41
42 @Override
43 public String toString() {
44 return make + " " + model + " " + year;
45 }
46
47 @Override
48 public boolean equals(Object object) {
49 if (object == this) {
50 return true;
51 }
52
53 if (null == object || !(object instanceof Car)) {
54 return false;
55 }
56
57 var that = (Car)object;
58 if (this.make.equals(that.make)
59 && this.model.equals(that.model)
60 && this.year == that.year
61 && this.milesPerGallon == that.milesPerGallon) {
62 return true;
63 }
64
65 return false;
66 }
67
68 @Override
69 public int hashCode() {
70 int result = 17;
71 result = 31 * result + make.hashCode();
72 result = 31 * result + model.hashCode();
73 result = 31 * result + (new Integer(year)).hashCode();
74 result = 31 * result + (new Double(milesPerGallon).hashCode());
75 return result;
76 }
77
78 public static Car getBetterMileage(Car lhs, Car rhs) {
79 return lhs.milesPerGallon > rhs.milesPerGallon ? lhs : rhs;
80 }
81}
1Car car1 = new Car("Honda", "Accord", 2019, 22.0);
2Car car2 = new Car("Honda", "Civic", 2019, 25.0);
3
4System.out.println(car1 + " vs " + car2);
5System.out.println(Car.getBetterMileage(car1, car2) + " has better mileage");
6.6. Methods with variable-length arguments
1public static class Car {
2 private String make;
3 private String model;
4 private int year;
5 private double milesPerGallon;
6 private String[] passengers;
7
8 public Car() {
9 this("None", "None", -1, 0.0d);
10 }
11
12 public Car(String make, String model, int year, double milesPerGallon) {
13 this.make = make;
14 this.model = model;
15 this.year = year;
16 this.milesPerGallon = milesPerGallon;
17 }
18
19 public String getMake() {
20 return make;
21 }
22
23 public String getModel() {
24 return model;
25 }
26
27 public int getYear() {
28 return year;
29 }
30
31 public double getMilesPerGallon() {
32 return milesPerGallon;
33 }
34
35 public double getMiles(double gallons) {
36 return milesPerGallon * gallons;
37 }
38
39 public double getGallons(double miles) {
40 return (1.0 / milesPerGallon) * miles;
41 }
42
43 public String[] getPassengers() {
44 return this.passengers;
45 }
46
47 public void setPassengers(String ... passengers) {
48 this.passengers = passengers;
49 }
50
51 @Override
52 public String toString() {
53 return make + " " + model + " " + year;
54 }
55
56 @Override
57 public boolean equals(Object object) {
58 if (object == this) {
59 return true;
60 }
61
62 if (null == object || !(object instanceof Car)) {
63 return false;
64 }
65
66 var that = (Car)object;
67 if (this.make.equals(that.make)
68 && this.model.equals(that.model)
69 && this.year == that.year
70 && this.milesPerGallon == that.milesPerGallon) {
71 return true;
72 }
73
74 return false;
75 }
76
77 @Override
78 public int hashCode() {
79 int result = 17;
80 result = 31 * result + make.hashCode();
81 result = 31 * result + model.hashCode();
82 result = 31 * result + (new Integer(year)).hashCode();
83 result = 31 * result + (new Double(milesPerGallon).hashCode());
84 return result;
85 }
86
87 public static Car getBetterMileage(Car lhs, Car rhs) {
88 return lhs.milesPerGallon > rhs.milesPerGallon ? lhs : rhs;
89 }
90}
1Car car = new Car("Honda", "Accord", 2019, 22.0);
2car.setPassengers("John", "Joe", "Jane", "Joyce");
3
4for (String passenger : car.getPassengers()) {
5 System.out.println(passenger);
6}
6.7. Inheritance
1public static abstract class Pet {
2 protected final String name;
3
4 public Pet(String name) {
5 this.name = name;
6 }
7
8 public final String whoAmI() {
9 return "Pet";
10 }
11
12 public abstract String getNoise();
13
14 @Override
15 public String toString() {
16 return name;
17 }
18}
19
20public static class Dog extends Pet {
21 public Dog(String name) {
22 super(name);
23 }
24
25 @Override
26 public String getNoise() {
27 return "woof!";
28 }
29
30 @Override
31 public String toString() {
32 return "Dog: " + name;
33 }
34}
35
36public static class Cat extends Pet {
37 public Cat(String name) {
38 super(name);
39 }
40
41 @Override
42 public String getNoise() {
43 return "meow!";
44 }
45
46 @Override
47 public String toString() {
48 return "Cat: " + name;
49 }
50}
1Pet dog = new Dog("Max");
2Pet cat = new Cat("Nancy");
3
4System.out.println(dog + " says " + dog.getNoise());
5System.out.println(cat + " says " + cat.getNoise());
6
7System.out.println(dog + " is a " + dog.whoAmI());
8System.out.println(cat + " is a " + cat.whoAmI());
6.8. Interface
6.8.1. Basic interface
1public interface Pet {
2 public String getName();
3 public String getNoise();
4}
6.8.2. Nested interface
1public interface Pet {
2 public interface TYPE {
3 public int DOG = 1;
4 public int CAT = 2;
5 }
6 public String getName();
7 public String getNoise();
8 public int getType();
9}
6.8.3. Interface implementation
1public interface Pet {
2 public interface TYPE {
3 public int DOG = 1;
4 public int CAT = 2;
5 }
6 public String getName();
7 public String getNoise();
8 public int getType();
9}
10
11public static abstract class AbstractPet implements Pet {
12 protected final String name;
13 protected final int type;
14
15 public AbstractPet(String name, int type) {
16 this.name = name;
17 this.type = type;
18 }
19
20 public String getName() { return name; }
21 public int getType() { return type; }
22
23 @Override
24 public String toString() {
25 return name;
26 }
27}
28
29public static class Dog extends AbstractPet {
30 public Dog(String name) {
31 super(name, Pet.TYPE.DOG);
32 }
33
34 @Override
35 public String getNoise() {
36 return "woof!";
37 }
38}
39
40public static class Cat extends AbstractPet {
41 public Cat(String name) {
42 super(name, Pet.TYPE.CAT);
43 }
44
45 @Override
46 public String getNoise() {
47 return "meow!";
48 }
49}
1var pets = new Pet[] { new Dog("Max"), new Cat("Nancy") };
2
3for (Pet pet : pets) {
4 System.out.println(pet + " says " + pet.getNoise());
5}
6.8.4. More on interface
1public interface Pet {
2 public interface TYPE {
3 public int DOG = 1;
4 public int CAT = 2;
5 }
6 public String getName();
7 public String getNoise();
8 public int getType();
9
10 default String whoAmI() {
11 return "Pet";
12 }
13
14 static String getNaturalType(int type) {
15 switch (type) {
16 case TYPE.DOG:
17 return "Dog";
18 case TYPE.CAT:
19 return "Cat";
20 default:
21 return "Uknown";
22 }
23 }
24}
25
26public static abstract class AbstractPet implements Pet {
27 protected final String name;
28 protected final int type;
29
30 public AbstractPet(String name, int type) {
31 this.name = name;
32 this.type = type;
33 }
34
35 public String getName() { return name; }
36 public int getType() { return type; }
37
38 @Override
39 public String toString() {
40 return name;
41 }
42}
43
44public static class Dog extends AbstractPet {
45 public Dog(String name) {
46 super(name, Pet.TYPE.DOG);
47 }
48
49 @Override
50 public String whoAmI() {
51 return "Dog";
52 }
53
54 @Override
55 public String getNoise() {
56 return "woof!";
57 }
58}
59
60public static class Cat extends AbstractPet {
61 public Cat(String name) {
62 super(name, Pet.TYPE.CAT);
63 }
64
65 @Override
66 public String getNoise() {
67 return "meow!";
68 }
69}
1var pets = new Pet[] { new Dog("Max"), new Cat("Nancy") };
2
3for (Pet pet : pets) {
4 System.out.println(pet + " says " + pet.getNoise());
5 System.out.println("\t" + pet + " is a " + pet.whoAmI());
6 System.out.println("\t" + pet + " is naturally a " + Pet.getNaturalType(pet.getType()));
7}