Object orientation

Contents
CLASS variables
SELECT TYPE
Polymorphic arrays
Design small hierarchies

CLASS variables

A variable declared with class instead of type can hold a value of the named type or of any type that extends it.

Such a polymorphic variable is usually allocatable, and the allocate statement chooses the concrete dynamic type at run time.

The fragment below declares a polymorphic counter and stores an extended value in it.

program demo implicit none use named_counters class(counter), allocatable :: obj allocate(named_counter :: obj) print *, "ready" end program demo

SELECT TYPE

A select type block tests the dynamic type of a polymorphic variable and gives a typed name to each branch.

Inside a type is branch the variable behaves like that concrete type, so child components are directly visible.

The example prints the label only when the stored value really is a named counter.

select type (obj) type is (named_counter) print *, obj%label type is (counter) print *, obj%n class default print *, "unknown type" end select

Polymorphic arrays

An allocatable array of class elements can mix several child types in one container, which suits small plugin style lists.

Every element keeps its own dynamic type, and select type recovers the concrete view whenever child details matter.

The fragment allocates a two element team and calls the shared reset method on each member.

program demo implicit none use named_counters class(counter), allocatable :: team(:) allocate(counter :: team(2)) call team(1)%reset() call team(2)%reset() print *, size(team) end program demo

Design small hierarchies

Keep hierarchies shallow: one abstract parent with a few concrete children covers most Fortran programs without deep trees.

Put shared state and shared methods in the parent, and let each child add only the data and overrides that it truly needs.

Prefer composition with nested types when a plain has-a relation fits better than an is-a relation, since flat code is easier to test.

program demo implicit none use named_counters type(named_counter) :: job job%n = 3 job%label = "uploads" call job%reset() print *, job%n, job%label end program demo

Next: Operator overloading

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: