Skip to content

Commit 314e214

Browse files
authored
BAEL-1326 2nd attempt (eugenp#3281)
* BAEL-973: updated README * BAEL-1069: Updated README * BAEL-817: add README file * BAEL-1084: README update * BAEL-960: Update README * BAEL-1155: updated README * BAEL-1041: updated README * BAEL-973: Updated README * BAEL-1187: updated README * BAEL-1183: Update README * BAEL-1133: Updated README * BAEL-1098: README update * BAEL-719: add README.md * BAEL-1272: README update * BAEL-1272: README update * BAEL-1196: Update README * BAEL-1326: Default and static methods in interfaces
1 parent 52edd46 commit 314e214

7 files changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.defaultstaticinterfacemethods.application;
2+
3+
import com.baeldung.defaultstaticinterfacemethods.model.Car;
4+
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
5+
6+
public class Application {
7+
8+
public static void main(String[] args) {
9+
10+
Vehicle car = new Car("BMW");
11+
System.out.println(car.getBrand());
12+
System.out.println(car.speedUp());
13+
System.out.println(car.slowDown());
14+
System.out.println(car.turnAlarmOn());
15+
System.out.println(car.turnAlarmOff());
16+
System.out.println(Vehicle.getHorsePower(2500, 480));
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public interface Alarm {
4+
5+
default String turnAlarmOn() {
6+
return "Turning the alarm on.";
7+
}
8+
9+
default String turnAlarmOff() {
10+
return "Turning the alarm off.";
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class Car implements Vehicle {
4+
5+
private final String brand;
6+
7+
public Car(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The car is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The car is slowing down.";
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class Motorbike implements Vehicle {
4+
5+
private final String brand;
6+
7+
public Motorbike(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The motorbike is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The motorbike is slowing down.";
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public class MultiAlarmCar implements Vehicle, Alarm {
4+
5+
private final String brand;
6+
7+
public MultiAlarmCar(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String speedUp() {
18+
return "The motorbike is speeding up.";
19+
}
20+
21+
@Override
22+
public String slowDown() {
23+
return "The mootorbike is slowing down.";
24+
}
25+
26+
@Override
27+
public String turnAlarmOn() {
28+
return Vehicle.super.turnAlarmOn() + " " + Alarm.super.turnAlarmOn();
29+
}
30+
31+
@Override
32+
public String turnAlarmOff() {
33+
return Vehicle.super.turnAlarmOff() + " " + Alarm.super.turnAlarmOff();
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.defaultstaticinterfacemethods.model;
2+
3+
public interface Vehicle {
4+
5+
String getBrand();
6+
7+
String speedUp();
8+
9+
String slowDown();
10+
11+
default String turnAlarmOn() {
12+
return "Turning the vehice alarm on.";
13+
}
14+
15+
default String turnAlarmOff() {
16+
return "Turning the vehicle alarm off.";
17+
}
18+
19+
static int getHorsePower(int rpm, int torque) {
20+
return (rpm * torque) / 5252;
21+
}
22+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.defaultistaticinterfacemethods.test;
2+
3+
import com.baeldung.defaultstaticinterfacemethods.model.Car;
4+
import com.baeldung.defaultstaticinterfacemethods.model.Motorbike;
5+
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import static org.assertj.core.api.Assertions.*;
9+
10+
public class StaticDefaulInterfaceMethodUnitTest {
11+
12+
private static Car car;
13+
private static Motorbike motorbike;
14+
15+
@BeforeClass
16+
public static void setUpCarInstance() {
17+
car = new Car("BMW");
18+
}
19+
20+
@BeforeClass
21+
public static void setUpMotorbikeInstance() {
22+
motorbike = new Motorbike("Yamaha");
23+
}
24+
25+
@Test
26+
public void givenCarInstace_whenBrandisBMW_thenOneAssertion() {
27+
assertThat(car.getBrand()).isEqualTo("BMW");
28+
}
29+
30+
@Test
31+
public void givenCarInstance_whenCallingSpeedUp_thenOneAssertion() {
32+
assertThat(car.speedUp()).isEqualTo("The car is speeding up.");
33+
}
34+
35+
@Test
36+
public void givenCarInstance_whenCallingSlowDown_thenOneAssertion() {
37+
assertThat(car.slowDown()).isEqualTo("The car is slowing down.");
38+
}
39+
40+
@Test
41+
public void givenCarInstance_whenCallingTurnAlarmOn_thenOneAssertion() {
42+
assertThat(car.turnAlarmOn()).isEqualTo("Turning the vehice alarm on.");
43+
}
44+
45+
@Test
46+
public void givenCarInstance_whenCallingTurnAlarmOff_thenOneAssertion() {
47+
assertThat(car.turnAlarmOff()).isEqualTo("Turning the vehicle alarm off.");
48+
}
49+
50+
@Test
51+
public void givenVehicleInterface_whenCallinggetHorsePower_thenOneAssertion() {
52+
assertThat(Vehicle.getHorsePower(2500, 480)).isEqualTo(228);
53+
}
54+
55+
@Test
56+
public void givenMooorbikeInstace_whenBrandisYamaha_thenOneAssertion() {
57+
assertThat(motorbike.getBrand()).isEqualTo("Yamaha");
58+
}
59+
60+
@Test
61+
public void givenMotorbikeInstance_whenCallingSpeedUp_thenOneAssertion() {
62+
assertThat(motorbike.speedUp()).isEqualTo("The motorbike is speeding up.");
63+
}
64+
65+
@Test
66+
public void givenMotorbikeInstance_whenCallingSlowDown_thenOneAssertion() {
67+
assertThat(motorbike.slowDown()).isEqualTo("The motorbike is slowing down.");
68+
}
69+
70+
@Test
71+
public void givenMotorbikeInstance_whenCallingTurnAlarmOn_thenOneAssertion() {
72+
assertThat(motorbike.turnAlarmOn()).isEqualTo("Turning the vehice alarm on.");
73+
}
74+
75+
@Test
76+
public void givenMotorbikeInstance_whenCallingTurnAlarmOff_thenOneAssertion() {
77+
assertThat(motorbike.turnAlarmOff()).isEqualTo("Turning the vehicle alarm off.");
78+
}
79+
}

0 commit comments

Comments
 (0)