# Encapsulation and Interface-like implementations in Julia

**URL:** <https://discourse.julialang.org/t/encapsulation-and-interface-like-implementations-in-julia/74616>\
**Category:** New to Julia\
**Tags:** design-pattern\
**Created:** [January 14, 2022, 12:39pm UTC](https://discourse.julialang.org/t/encapsulation-and-interface-like-implementations-in-julia/74616 "2022-01-14T12:39:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [January 14, 2022, 1:07pm UTC](https://discourse.julialang.org/t/encapsulation-and-interface-like-implementations-in-julia/74616/2 "2022-01-14T13:07:27Z")

</div>

This Invenia blog post is a very good read on this topic in my opinion:

> **[Development with Interface Packages](https://invenia.github.io/blog/2020/11/06/interfacetesting/)**
>
> Over the last two years, our Julia codebase has grown in size and complexity, and is now the centerpiece of both our operations and research. This implies that we need to routinely replace parts of the system like puzzle pieces, and carefully test if...

  

In the case of your example, one simple way to at least document the `Animal` interface would be via bare function definitions with docstrings. Something along the lines of:

```julia
abstract type Animal end

"return the name of an animal"
function get_name end # notice the absence of parentheses. No method is attached to this function (yet)

"make an animal sound"
function make_sound end

function greet(animal::Animal) # this default method uses the previously defined functions
    println("hello "+ get_name(animal))
    make_sound(animal);
end

```

And then any animal implementor can at least have an idea of what to implement by looking at the source code for `Animal`:

```julia
struct Dog <: Animal name::String end

# these define a specific methods for the functions declared above
get_name(dog::Dog) = dog.name
make_sound(dog::Dog) = println(get_name(dog) + " barks!")

# ... and we get a default `greet` implementation for free

```

  

But apart from custom test utilities such as those mentioned in Invenia’s blog post above, most of this relies on good documentation. Examples of well documented interfaces can be found in the Julia manual:

[https://docs.julialang.org/en/v1/manual/interfaces/](https://docs.julialang.org/en/v1/manual/interfaces/)

---

_[View the full topic](https://discourse.julialang.org/t/encapsulation-and-interface-like-implementations-in-julia/74616)._
