# Private and protected attributes in OOP

**URL:** <https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104>\
**Category:** New to Julia\
**Created:** [August 23, 2023, 7:16am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104 "2023-08-23T07:16:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed-desoki](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ahmed-desoki](https://discourse.julialang.org/u/ahmed-desoki)\
**Post date:** [August 23, 2023, 7:16am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/1 "2023-08-23T07:16:46Z")

</div>

Dear folks

I want to switch OOP from C++ to Julia. Please guide what is the Julia way of OOP?

In OOP in C++, classes can have member variables, or properties. These properties can have attributes like “private” or “protected”. Also member functions, or methods, can have these attributes.

In Julia, as far as I know, there are stucts or composite types, but no classes. There are no methods, but there is multiple dispatch.

My question, is there “private” and “protected” attributes for properties or methods? Can a function be a friend to a class (as C++) so that this function can access the private/protected properties?

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [August 23, 2023, 7:28am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/2 "2023-08-23T07:28:44Z")

</div>

> [@ahmed-desoki](#):
>
> There are no methods, but there is multiple dispatch.

There are no methods bound to a specific type as in class-based OOP languages, but they’re still called methods since they can be inherited from abstract super-types.

> [@ahmed-desoki](#):
>
> My question, is there “private” and “protected” attributes for properties or methods?

Currently no, everything is essentially public (though you can use underscores to discourage access, as in Python).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 23, 2023, 7:32am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/3 "2023-08-23T07:32:39Z")

</div>

It’s not really feasible to do OOP in Julia, classes just don’t exist. There’s types, but they’re not namespaces like classes are in other languages. Julia modules are namespaces, and the exports are somewhat like public properties.

However, true access modifiers do not exist, it’s more of a recommendation. Your public API can provide getter methods preferred over internal fields, you can overload `Base.getproperty` to affect what `instance.field` syntax does, but there are commonplace methods to access any property you want. This isn’t a characteristic of non-OOP, Python is OO and also lacks true access modifiers.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 23, 2023, 8:07am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/4 "2023-08-23T08:07:25Z")

</div>

See ObjectOriented.jl

[https://suzhou-tongyuan.github.io/ObjectOriented.jl/dev/](https://suzhou-tongyuan.github.io/ObjectOriented.jl/dev/)

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [August 23, 2023, 8:48am UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/5 "2023-08-23T08:48:48Z")

</div>

You may change your way of thinking in Julia. Modules are namespaces, define your types using structs, use abstract data types for inheritence. Use multiple-dispatch to define functions that work on your specific type. That’s

```julia
func(obj, params)

```

instead

```julia
obj.func(params)

```

You will end up with a clean design and see how things are better managed.

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [August 23, 2023, 1:31pm UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/6 "2023-08-23T13:31:40Z")

</div>

As others in this thread have said, Julia is not an object oriented language, so to write good Julia code you will eventually need to change the way that you structure your code.

Specifically private/protected fields can often be approximated with closures (but note that nothing is completely private in Julia, as far as I know, internal fields in closures can still be accessed).

This is my comment in a similar thread:

> [@Why there is no OOP (object oriented programming) in Julia?](https://discourse.julialang.org/t/why-there-is-no-oop-object-oriented-programming-in-julia/86723/48):
>
> So far, no-one here has suggested using “closures” which I personally find ideal for having specific data be associated with a specific function. A search for Julia and closures should give you a lot of references, one of them is [Julia Language Tutorial =\> Introduction to Closures](https://riptutorial.com/julia-lang/example/23207/introduction-to-closures) . It is a functional programming technique and if I understand you correctly, then this can provide what you are asking for. I use this technique a lot in places where I would have encapsulated things in an object in …

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [August 23, 2023, 2:06pm UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/7 "2023-08-23T14:06:35Z")

</div>

We are working on an alias inside VSCode for OOP habit of dot at [Feat/methodswith dot completion by xgdgsc · Pull Request #3240 · julia-vscode/julia-vscode · GitHub](https://github.com/julia-vscode/julia-vscode/pull/3240) .

---

<div class="post-metadata">

**Author:** ![ahmed-desoki](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ahmed-desoki](https://discourse.julialang.org/u/ahmed-desoki)\
**Post date:** [August 26, 2023, 2:30pm UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/8 "2023-08-26T14:30:44Z")

</div>

Thanks you all folks for your keen replies.

Please let me summarize all your replies;

1. Julia is not an OOP language
2. Composite type and/or structs can have properties
3. Properties cannot have attributes such as private, protected, static, …

Please correct any thing wrong, especially regarding the “static” attribute.

Thanks

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [August 26, 2023, 2:47pm UTC](https://discourse.julialang.org/t/private-and-protected-attributes-in-oop/103104/9 "2023-08-26T14:47:05Z")

</div>

> [@ahmed-desoki](#):
>
> 1. Properties cannot have attributes such as private, protected, static, …

The properties of ordinary structs are immutable and cannot have attributes. It is possible to declare a struct mutable and in this case any property can have the const attribute making it immutable, e.g.

```julia
mutable struct Abc
  const a::Integer
  b
  c::Float64
end

```
