# Delegation and method visibility

**URL:** <https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341>\
**Category:** Language Design\
**Created:** [March 2, 2017, 9:09am UTC](https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341 "2017-03-02T09:09:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [March 2, 2017, 9:09am UTC](https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341/1 "2017-03-02T09:09:39Z")

</div>

Hello,

I have some little wish concerning delegation. So I’m sharing the idea with this post. To explain the issue I’m referring to the delegation sample in the Kotlin documentation: [https://kotlinlang.org/docs/reference/delegation.html](https://kotlinlang.org/docs/reference/delegation.html)

In this sample there is this code:

> fun main(args: Array) {  
> val b = BaseImpl(10)  
> Derived(b).print() // prints 10  
> }

Now what I would like to have is the option of fun print() being protected inside class Derived. For class BaseImpl it remains public, of course. The point is that I only want to make use of the BaseImpl.print() method visible inside Derived through delegation, but I don’t want its public visibility to propagate to class Derived, because this breaks the encapsulation of Derived for me. To make sure print cannot be invoked on Derived as in

> Derived(b).print()

I have to write a wrapper around Derived that does not declare print another time or at least not as protected which is tedious and repeals the use of delegation in this situation.

So the idea is some way to control the visibility of public methods “obtained” through delegation. I think it would be usefull. What do other people think about this?

Regards, Oliver

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 2, 2017, 12:53pm UTC](https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341/2 "2017-03-02T12:53:10Z")

</div>

Actually delegation has nothing to do with it. Derived implements the Base interface where it is public. Even if you could do what you asked you could still do:

```
(Derived(10) as Base).print()

```

The idea is NOT some way to control the visibility of public methods “obtained” through delegation, but controlling visibility of public methods inherited by implementing an interface and the answer is you can’t.

---

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [March 2, 2017, 1:15pm UTC](https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341/3 "2017-03-02T13:15:03Z")

</div>

> [@dalewking](#):
>
> (Derived(10) as Base).print()

Yeah, I thought about that as well. But from the way I understand it only BaseImpl implements Base whereas Derived does not. I see no reason Derived would have to, because this would render delegation meaningless: I would just use JDK8 default interface methods in Base and had the same effect without having to go through delegation.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 2, 2017, 2:02pm UTC](https://discuss.kotlinlang.org/t/delegation-and-method-visibility/2341/4 "2017-03-02T14:02:42Z")

</div>

> [@Oliver\_Plohmann](#):
>
> Yeah, I thought about that as well. But from the way I understand it only BaseImpl implements Base whereas Derived does not.[/quote]
> 
> It certainly does in the example
> 
> > [@](#):
> >
> > I see no reason Derived would have to,
> 
> It has to because that is how Kotlin defines it. You can implement an interface by delegating to an instance that implements that interface.
> 
> > [@](#):
> >
> > because this would render delegation meaningless:
> 
> Nonsense it is still extremely powerful. It is built for the case where you want to wrap an existing implementation of an interface but modify only a small subset of the functionality.
> 
> [quote]I would just use JDK8 default interface methods in Base and had the same effect without having to go through delegation.

That is not the same thing at all and as Kotlin was built to be JDK6 compliant not a possibility
