Skip to content

Commit 94f4092

Browse files
kyleandarimaibin
authored andcommitted
BAEL-2431 Implementing multi-inheritance and Polymorphism using interfaces (eugenp#5924)
* Implementing Hexagonal Architecture in java * Implementing multi-inheritance and Polymorphism using interfaces * Removing hexagonal code * Deleting hexagonal architecture code * fix for unit test names
1 parent 1fc9300 commit 94f4092

9 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.interfaces.multiinheritance;
2+
3+
public abstract interface Fly{
4+
void fly();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.interfaces.multiinheritance;
2+
3+
public interface Transform {
4+
void transform();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.interfaces.multiinheritance;
2+
3+
public class Vehicle implements Fly, Transform {
4+
@Override
5+
public void fly() {
6+
System.out.println("I can Fly!!");
7+
}
8+
9+
@Override
10+
public void transform() {
11+
System.out.println("I can Transform!!");
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.interfaces.polymorphysim;
2+
3+
public class Circle implements Shape {
4+
5+
private double radius;
6+
7+
public Circle(double radius){
8+
this.radius = radius;
9+
}
10+
11+
@Override
12+
public String name() {
13+
return "Circle";
14+
}
15+
16+
@Override
17+
public double area() {
18+
return Math.PI * (radius * radius);
19+
}
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.interfaces.polymorphysim;
2+
3+
import java.util.ArrayList;
4+
5+
public class DisplayShape {
6+
7+
private ArrayList<Shape> shapes;
8+
9+
public DisplayShape() {
10+
shapes = new ArrayList<>();
11+
}
12+
13+
public void add(Shape shape) {
14+
shapes.add(shape);
15+
}
16+
17+
public void display() {
18+
for (Shape shape : shapes) {
19+
System.out.println(shape.name() + " area: " + shape.area());
20+
}
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.interfaces.polymorphysim;
2+
3+
public class MainPolymorphic {
4+
public static void main(String[] args){
5+
6+
Shape circleShape = new Circle(2);
7+
Shape squareShape = new Square(2);
8+
9+
DisplayShape displayShape = new DisplayShape();
10+
displayShape.add(circleShape);
11+
displayShape.add(squareShape);
12+
13+
displayShape.display();
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.interfaces.polymorphysim;
2+
3+
public interface Shape {
4+
public abstract String name();
5+
public abstract double area();
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.interfaces.polymorphysim;
2+
3+
public class Square implements Shape {
4+
5+
private double width;
6+
7+
public Square(double width) {
8+
this.width = width;
9+
}
10+
11+
@Override
12+
public String name() {
13+
return "Square";
14+
}
15+
16+
@Override
17+
public double area() {
18+
return width * width;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.interfaces;
2+
3+
import com.baeldung.interfaces.polymorphysim.Circle;
4+
import com.baeldung.interfaces.polymorphysim.Shape;
5+
import com.baeldung.interfaces.polymorphysim.Square;
6+
import org.assertj.core.api.Assertions;
7+
import org.junit.Test;
8+
9+
public class PolymorphysimUnitTest {
10+
11+
@Test
12+
public void whenInterfacePointsToCircle_CircleAreaMethodisBeingCalled(){
13+
double expectedArea = 12.566370614359172;
14+
Shape circle = new Circle(2);
15+
double actualArea = circle.area();
16+
Assertions.assertThat(actualArea).isEqualTo(expectedArea);
17+
}
18+
19+
@Test
20+
public void whenInterfacePointsToSquare_SquareAreaMethodisBeingCalled(){
21+
double expectedArea = 4;
22+
Shape square = new Square(2);
23+
double actualArea = square.area();
24+
Assertions.assertThat(actualArea).isEqualTo(expectedArea);
25+
}
26+
}

0 commit comments

Comments
 (0)