BAEL-2431 Implementing multi-inheritance and Polymorphism using interfaces (#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
This commit is contained in:
kyleandari 2018-12-15 17:35:09 -05:00 committed by maibin
parent 1fc93000b6
commit 94f4092314
9 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.interfaces.multiinheritance;
public abstract interface Fly{
void fly();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.interfaces.multiinheritance;
public interface Transform {
void transform();
}

View File

@ -0,0 +1,13 @@
package com.baeldung.interfaces.multiinheritance;
public class Vehicle implements Fly, Transform {
@Override
public void fly() {
System.out.println("I can Fly!!");
}
@Override
public void transform() {
System.out.println("I can Transform!!");
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.interfaces.polymorphysim;
public class Circle implements Shape {
private double radius;
public Circle(double radius){
this.radius = radius;
}
@Override
public String name() {
return "Circle";
}
@Override
public double area() {
return Math.PI * (radius * radius);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.interfaces.polymorphysim;
import java.util.ArrayList;
public class DisplayShape {
private ArrayList<Shape> shapes;
public DisplayShape() {
shapes = new ArrayList<>();
}
public void add(Shape shape) {
shapes.add(shape);
}
public void display() {
for (Shape shape : shapes) {
System.out.println(shape.name() + " area: " + shape.area());
}
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.interfaces.polymorphysim;
public class MainPolymorphic {
public static void main(String[] args){
Shape circleShape = new Circle(2);
Shape squareShape = new Square(2);
DisplayShape displayShape = new DisplayShape();
displayShape.add(circleShape);
displayShape.add(squareShape);
displayShape.display();
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.interfaces.polymorphysim;
public interface Shape {
public abstract String name();
public abstract double area();
}

View File

@ -0,0 +1,20 @@
package com.baeldung.interfaces.polymorphysim;
public class Square implements Shape {
private double width;
public Square(double width) {
this.width = width;
}
@Override
public String name() {
return "Square";
}
@Override
public double area() {
return width * width;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.interfaces;
import com.baeldung.interfaces.polymorphysim.Circle;
import com.baeldung.interfaces.polymorphysim.Shape;
import com.baeldung.interfaces.polymorphysim.Square;
import org.assertj.core.api.Assertions;
import org.junit.Test;
public class PolymorphysimUnitTest {
@Test
public void whenInterfacePointsToCircle_CircleAreaMethodisBeingCalled(){
double expectedArea = 12.566370614359172;
Shape circle = new Circle(2);
double actualArea = circle.area();
Assertions.assertThat(actualArea).isEqualTo(expectedArea);
}
@Test
public void whenInterfacePointsToSquare_SquareAreaMethodisBeingCalled(){
double expectedArea = 4;
Shape square = new Square(2);
double actualArea = square.area();
Assertions.assertThat(actualArea).isEqualTo(expectedArea);
}
}