In Fortran, we can define abstract interfaces that are similar to Go’s interfaces. However, Fortran uses abstract types and type-bound procedures to achieve similar functionality. Let’s see how we can implement the geometry example in Fortran.
In this Fortran implementation:
We define an abstract type geometry with deferred procedures area and perim.
We create concrete types rect and circle that extend the geometry type and implement the required procedures.
The measure subroutine takes a geometry class argument, allowing it to work with any type that extends geometry.
In the main program, we create instances of rect and circle and pass them to the measure subroutine.
To compile and run this Fortran program:
This will output the area and perimeter of the rectangle and circle.
While Fortran doesn’t have interfaces in the same way as Go, abstract types and type-bound procedures provide similar functionality, allowing for polymorphic behavior and code reuse.