File tree Expand file tree Collapse file tree
main/java/com/baeldung/interfaces
test/java/com/baeldung/interfaces Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .interfaces .multiinheritance ;
2+
3+ public abstract interface Fly {
4+ void fly ();
5+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .interfaces .multiinheritance ;
2+
3+ public interface Transform {
4+ void transform ();
5+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .interfaces .polymorphysim ;
2+
3+ public interface Shape {
4+ public abstract String name ();
5+ public abstract double area ();
6+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments