diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java new file mode 100644 index 0000000000..d84182aec6 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java @@ -0,0 +1,5 @@ +package com.baeldung.interfaces.multiinheritance; + +public abstract interface Fly{ + void fly(); +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java new file mode 100644 index 0000000000..a18bbafdc1 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java @@ -0,0 +1,5 @@ +package com.baeldung.interfaces.multiinheritance; + +public interface Transform { + void transform(); +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java new file mode 100644 index 0000000000..fb0d36e3e0 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java @@ -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!!"); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java new file mode 100644 index 0000000000..bf0e613567 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java @@ -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); + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java new file mode 100644 index 0000000000..2cf4fafee1 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java @@ -0,0 +1,22 @@ +package com.baeldung.interfaces.polymorphysim; + +import java.util.ArrayList; + +public class DisplayShape { + + private ArrayList 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()); + } + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/MainPolymorphic.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/MainPolymorphic.java new file mode 100644 index 0000000000..cc43c1300b --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/MainPolymorphic.java @@ -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(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java new file mode 100644 index 0000000000..fcb0c65e7b --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java @@ -0,0 +1,6 @@ +package com.baeldung.interfaces.polymorphysim; + +public interface Shape { + public abstract String name(); + public abstract double area(); +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java new file mode 100644 index 0000000000..9c440150b5 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java @@ -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; + } +} diff --git a/core-java-8/src/test/java/com/baeldung/interfaces/PolymorphysimUnitTest.java b/core-java-8/src/test/java/com/baeldung/interfaces/PolymorphysimUnitTest.java new file mode 100644 index 0000000000..7ded5e6621 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/interfaces/PolymorphysimUnitTest.java @@ -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); + } +}