A derived type is extensible if it has neither the bind
attribute nor the sequence
attribute. Such a type may be extended by another type.
module mod
type base_type
integer i
end type base_type
type, extends(base_type) :: higher_type
integer j
end type higher_type
end module mod
A polymorphic variable with declared type base_type
is type compatible with type higher_type
and may have that as dynamic type
class(base_type), allocatable :: obj
allocate(obj, source=higher_type(1,2))
Type compatability descends through a chain of children, but a type may extend only one other type.
An extending derived type inherits type bound procedures from the parent, but this can be overriden
module mod
type base_type
contains
procedure :: sub => sub_base
end type base_type
type, extends(base_type) :: higher_type
contains
procedure :: sub => sub_higher
end type higher_type
contains
subroutine sub_base(this)
class(base_type) this
end subroutine sub_base
subroutine sub_higher(this)
class(higher_type) this
end subroutine sub_higher
end module mod
program prog
use mod
class(base_type), allocatable :: obj
obj = base_type()
call obj%sub
obj = higher_type()
call obj%sub
end program