Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 184.108.40.206 release ready!
Owner and Founder. Opencart
You will discover more about Kotlin “open” keyword in this blog. You’ll discover how to utilize the open keyword with the class name, function name, and variable name, as well as its benefits.
Everyone is aware that the foundation of every Object-Oriented Programming language is inheritance. It is the process of obtaining or applying the traits and qualities of one class through another. Now we are using a keyword in the kotlin class to achieve inheritance.
Since all classes in Kotlin are final by default, which means they cannot be inherited, we must apply the term open in your class to enable inheritance. Let’s use an example to verify.
To inherit one class from another in Kotlin, use the syntax shown below :
In the example above, the class Demo is inherited from the class Main. If you have a used without an keyword class to inherit with another class they will show a warning.
There won’t be an error after adding the keyword, so you can use inheritance with ease.
Kotlin requires explicit modifiers for overridable members and overrides. Like classes, all Kotlin functions are final by default, meaning that you can’t override a function that has a final nature.
In this code, you can see the parent class demo has a function ‘draw’ and the demo class extends with the main class. In the main class are override functions ‘draw’ now we are redefining the function body.
We learned about the ‘Open’ keyword used in kotlin class in this blog.
Visit the link for additional information on the Overriding methods in Kotlin by using keywords.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.
Always be ready for learning 🙂
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.