51

In Kotlin open is the same as not final in Java for classes and methods.

What does open give me in the following class for the field marked as open?

@MappedSuperclass
abstract class BaseEntity() : Persistable<Long> {
     open var id: Long? = null
}

updated this is not duplicate of What is the difference between 'open' and 'public' in Kotlin?

I am interested in open keyword for properties

updated

open class can be inherited.
open fun can be overridden
val property is final field in java

what about open property?

M.T
  • 881
  • 2
  • 10
  • 17
  • 3
    @s1m0nw1 it is not duplicate I am curious about `open` keyword for fields – M.T Mar 03 '18 at 01:33
  • 1
    Just an observation - I know nothing of Kotlin, so I am abstaining from voting. But, if you could provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) demonstrating its usage with properties (don't just add "updated" to the original question, change the *whole* question to specifically be about open vs non-open properties), this *might* be reopened because the other question does not seem to address it. Just be sure to keep the "this is not a dupe" text and link in the question, too. – NightOwl888 Mar 04 '18 at 03:23
  • 2
    The open keyword for a field allows the field visibility/content to be changed later on, and still keeping the same name: ```open class Container { protected open val fieldA: String = "Some value" } class DerivedContainer : Container() { public override val fieldA: String = "Something else" } ``` – Haomin Mar 29 '19 at 03:18
  • 4
    How is this question a duplicate of __difference between open and public__ ? – Mahdi-Malv Apr 06 '19 at 05:59
  • I feel it's really important to mention here that there is no such thing as "fields" in Kotlin. Kotlin has **properties**. This is a getter method for **val**s and a getter and a setter methods for **var**s defined in a class. Even though they have a backing *field* under the hood, we shouldn't bother about it. Now that this is mentioned, the "open" keyword for **properties** should make much more sense, since it simply states that the generated get/set methods won't be final and will be open for overriding. – dephinera Jan 25 '20 at 14:31
  • You can read this blog for more information regarding open keyword https://www.android4dev.com/what-is-open-class-in-android-kotlin/ – Lokesh Desai Jun 17 '20 at 03:11

2 Answers2

41

As you said, the open keyword allows you to override classes, when used in the class declaration. Accordingly, declaring a property as open, allows subclasses to override the property itself (e.g., redefine getter/setter). That keyword is required since in Kotlin everything is "final" by default, meaning that you can't override it (something similar to C#, if you have experience with that).

Note that your class is implicitly declared as open since it is abstract, hence you cannot create an instance of that class directly.

user2340612
  • 10,053
  • 4
  • 41
  • 66
  • I didn't get how is the class implicitly declared as open since it is abstract thing... – kevz Aug 28 '19 at 05:01
  • @kevz `abstract` means you can't create an instance of that class but you must subclass it first. Because of that, marking a class as `abstract` means the class is also `open` (otherwise you wouldn't be able to subclass it). Example: `abstract class Shape` can't be directly instantiated, but you can create and instantiate subclasses (e.g. `Triangle`, `Rectangle`, ...) – user2340612 Aug 28 '19 at 08:21
24

final method in Java: A method that cannot be overridden.

final class in Java: A class that cannot be extended.

Open classes and methods in Kotlin are equivalent to the opposite of final in Java, an open method is overridable and an open class is extendable in Kotlin.

Ivan Bila
  • 747
  • 7
  • 9
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • An open property must be a non-final field in Java – Lajos Arpad Mar 03 '18 at 13:29
  • It might be less confusing to define `open` without using a reference to java, especially since your answer seems to ignore the subtleties of this reserved word. – SMBiggs Aug 22 '19 at 05:26