# Abstract type and derived types - a problem with sample code

**URL:** <https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053>\
**Category:** Help\
**Created:** [March 27, 2022, 9:00pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053 "2022-03-27T21:00:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 27, 2022, 9:00pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/1 "2022-03-27T21:00:32Z")

</div>

I’ve found interesting the list compiled by @Beliavsky, of [Fortran tutorials](https://fortran-lang.discourse.group/t/tutorials-on-object-oriented-programming-in-fortran/3052/3) on OOP, in which I have little-to-no experience. I started to read Chapter 11 of [Holcomb’s tutorial](https://wiki.uiowa.edu/download/attachments/109785161/fortran-novella-Holcomb.pdf) and found something intriguing.  
There is an example on abstract types and inheritance, starting on page 89, with a definition of abstract type `animal`, followed by a ‘normal’ type, `mammal` which is announced in the last paragraph before the example as derived (it seems that from animal). The definition of `mammal` type, however, has no reference to `animal`, only the module in which it is embedded, `use`-s `animal` module. When the `module mammal_class` is extracted from the code, with `use animal_class` commented out, it compiles cleanly.  
I thought maybe the author just forgot to add `extends(animal)` to the `type mammal` but with this phrase added, compiler stops with the error:

```nohighlight
   36 | type, extends(animal) :: mammal
      | 1
Error: Derived-type 'mammal' declared at (1) must be ABSTRACT because 'print' is DEFERRED and not overridden

```

Any explanation would be welcomed. I am not including the code as I am not sure if it would be OK, so just the link to the full document.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 27, 2022, 9:28pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/2 "2022-03-27T21:28:26Z")

</div>

Maybe it can be useful.  
In “_Modern Fortran explained_” by Metcalf, section 15.10 pag 313:  
“_The use of the **abstract** and **deferred** attributes ensures that objects of insufficient type cannot be created, and that when the abstract type to create a normal type, the programmer can expect a diagnostic from the compiler if he or she has forgotten to override any inherent deferred type-bound procedures_”

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 27, 2022, 9:55pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/3 "2022-03-27T21:55:41Z")

</div>

I had this very page of MFE opened on my desk. But the example on that page clearly shows the derived type (my\_integer) is extending the abstract one (my\_numeric\_type).

I understand the error when `extends (animal)` is added to the Holcomb’s example but do not understand the idea of the types (mammal and all the other defined later which extend the mammal) if the mammal has no reference to the abstract type. Or, maybe, it does have but I don’t get how.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 27, 2022, 10:17pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/4 "2022-03-27T22:17:33Z")

</div>

Output from Holcomb’s example pag 89

`gfortran testabstract.f90 -o testabstract`

`Meow`  
` Woof`  
` Felis sylvestris catus`  
` I am Itty,I am a tuxedo`  
` I am Teddy, I am a poodle`

–

`GNU Fortran (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0`

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [March 27, 2022, 10:19pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/5 "2022-03-27T22:19:41Z")

</div>

It looks like an erroneous example to me.

The mammal derived type, should be really declared with:

```auto
type, abstract, extends(animal) :: mammal
  ! ...
end type

```

As far as I can tell, the idea was that the `print_species` is implemented at the level of mammal, while the `speak` and `print` procedures are implemented at the child level, since they are specific to each animal.

You can see in the `init_???` subroutines, that first a mammal instance is created using the structure-constructor:

```auto
type(cat) function init_cat(my_name,breed)
character(len=*), intent(in) :: breed
character(len=*), intent(in) :: my_name
init_cat%mammal=mammal('Felis sylvestris catus') ! reference to the parent type
init_cat%name=my_name
init_cat%breed=breed
end function init_cat

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [March 27, 2022, 10:55pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/6 "2022-03-27T22:55:13Z")

</div>

> [@msz59](#):
>
> … Any explanation would be welcomed …

TL;DR:

- An extended derived type is “concrete” if it does not have the `ABSTRACT` attribute,
- A “concrete” derived type must provide non-`DEFERRED` i.e., an overriding type-bound procedures of all the deferred bound procedures in the parent type i.e., actual implementations of any virtual methods stated in the base “class”
- An extended type which is itself `ABSTRACT` can “defer” such “implementations” to its extensions (aka its children)!

Thus if you have

```fortran
   type, abstract :: foo_t
      ..
   contains
      procedure(Ido_something), deferred :: do_something
      ..
   end type
   ..
   ! elided is abstract interface Ido_something

```

then a “concrete” extended type must override `do_something`:

```fortran
   type, extends(foo_t) :: bar_t
      ..
   contains
      procedure :: do_something => bar_do_something
      ..
   end type
   ..

```

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [March 27, 2022, 11:04pm UTC](https://fortran-lang.discourse.group/t/abstract-type-and-derived-types-a-problem-with-sample-code/3053/7 "2022-03-27T23:04:50Z")

</div>

Thanks to all. The general idea, as given by @FortranFan, I do know (maybe with an exception of that there can be an extended abstract type). My question was more specifically directed to the example in which I did not find the connection between the abstract base and the derived type. As @ivanpribec stated, somehow confirming my doubts, the example is probably erroneous (in the sense of illustrating the use of abstract types), although, and that is also interesting, it compiles and works fine even without the reference to the abstract base, as shown by @alozada
