- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does 'by' keyword do in Kotlin?
Kotlin supports delegation of design pattern by introducing a new keyword "by". Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object.
Example
In this example, we will implement an abstract method of a Base class from another class.
interface Base { //abstract method fun printMe() } class BaseImpl(val x: Int) : Base { // implementation of the method override fun printMe() { println(x) } } // delegating the public method on the object b class Derived(b: Base) : Base by b fun main(args: Array<String>) { val b = BaseImpl(10) // prints 10 :: accessing the printMe() method Derived(b).printMe() }
Output
It will produce the following output
10
- Related Articles
- What does an auto keyword do in C++?
- What does the keyword var do in C#?
- What does "?:" do in Kotlin? (Elvis Operator)
- What does the "===" operator do in Kotlin?
- How does the reified keyword work in Kotlin?
- What is "out" keyword in Kotlin?
- What does "return@" mean in Kotlin?
- What does the explicit keyword mean in C++?
- What does the KEY keyword mean in MySQL?
- What does the restrict keyword mean in C++?
- What does the volatile keyword mean in C++?
- What does "print >>" do in python?
- What does axes.flat in Matplotlib do?
- What does Tensor.detach() do in PyTorch?
- What does backward() do in PyTorch?

Advertisements