92

I am new to Kotlin and I am confused between open and public keywords. Could anyone please tell me the difference between those keywords?

Sagun Raj Lage
  • 2,326
  • 2
  • 18
  • 28
  • Read this article if you want more clarification on "open" keyword in Kotlin https://www.android4dev.com/what-is-open-class-in-android-kotlin/ – Lokesh Desai Jun 17 '20 at 03:14

8 Answers8

121

The open keyword means “open for extension“ - i.e. it's possible to create subclasses of an open class:

The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, Item 17: Design and document for inheritance or else prohibit it.

You also need to be explicit about methods you want to make overridable, also marked with open:

open class Base {
    open fun v() {}
    fun nv() {}
}

The public keyword acts as a visibility modifier that can be applied on classes, functions, member functions, etc. If a top-level class or function is public, it means it can be used from other files, including from other modules. Note that public is the default if nothing else is specified explicitly:

If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • What is the meaning of the phrase "your declarations will be visible everywhere"? – Sagun Raj Lage Feb 28 '18 at 07:49
  • That’s a quote from the docs, any class will have access and can create an instance: https://kotlinlang.org/docs/reference/visibility-modifiers.html – s1m0nw1 Feb 28 '18 at 07:53
  • 9
    Okay. Now I get it. 'Visible' refers to 'accessible'. And `public` makes it accessible from anywhere. `open` allows inheritance of a class. Thank you! – Sagun Raj Lage Feb 28 '18 at 07:56
33

class A { ... } in Java is equal to open class A { ... } in Kotlin.
final class B { ... } in Java is equal to class B { ...} in Kotlin.

It is not related with public.

In Kotlin, everything without access modifiers is public by default. You can explicitly say public in the definition, but it is not necessary in Kotlin.

So,

public class A { ... }

and

class A { ... }

are the same in Kotlin.

Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • 2
    If so, what exactly is the use of making a class `public`? If you use `open`, you can inherit the class. What is the main purpose of making a class `public`? – Sagun Raj Lage Feb 28 '18 at 07:46
  • 3
    I think it's like a semicolon in Kotlin. They exist for Java people. – Naetmul Apr 15 '21 at 06:23
15

I put here just for my memo, maybe useful for someone else :

open class in kotlin means that a class can be inherited because by default they are not:

 class Car{....}
 class Supercar:Car{....} : // give an error

 open Car2{....}
 class Supercar:Car2{....} : // ok

public class in Java is about the visibility of class (nothing to do with inheritance : unless a class in java is final, it can be inherited by default). In kotlin all the class are public by default.

open method in kotlin means that the method can be overridden, because by default they are not. Instead in Java all the methods can be overridden by default

The method of an open class cannot be overridden by default as usual (doesn't matter if the class is open), they must be declared that they can be overridden :

 open class Car{
    fun steering{...}
 }
 class Supercar:Car{
    override fun steering {...}  // give an error
 } 

 open class Car2{
    open fun steering{...}
 }
 class Supercar:Car2{
    override fun steering {...}  // ok
 }

for more details : https://kotlinlang.org/docs/reference/classes.html

android_dev71
  • 517
  • 5
  • 7
6

public: public keyword in Kotlin is similar to java it is use to make the visibility of classes, methods, variables to access from anywhere.

open: In Kotlin all classes, functions, and variables are by defaults final, and by inheritance property, we cannot inherit the property of final classes, final functions, and data members. So we use the open keyword before the class or function or variable to make inheritable that.

  • This is wrong, you still can inherit a visible non-`open` member however you cannot override it. – karakays Jan 04 '22 at 06:33
  • Please visit below link of Kotlin official website to clear your doubt. https://kotlinlang.org/docs/inheritance.html#:~:text=By%20default%2C%20Kotlin%20classes%20are%20final%20%E2%80%93%20they%20can%E2%80%99t%20be%20inherited.%20To%20make%20a%20class%20inheritable%2C%20mark%20it%20with%20the%20open%20keyword%3A – Sachidanand Pandit Jan 04 '22 at 10:05
  • Also when we make the parent class 'abstract' it will be inheritable. – Mohsen Emami Feb 04 '22 at 12:22
  • 1
    @karakays's comment is perhaps pedantic, but correct. A subclass always *inherits* all the parent class's members - that is, all methods and properties that exists on the parent class will also exist on the subclass. Applying the `open` modifier to class members determines whether they are *overridable*, not whether they are *inheritable*. The docs you link to in your reply to him corroborate his point. – Mark Amery Jun 03 '22 at 07:32
4

open is opposite to Final in java. If the class is not 'open', it can't be inherited.

class First{}
class Second:First(){}  // Not allowed. Since 'First' is Final(as in Java) by default. Unless marked "open" it can't be inherited 

Don't get confused with open and public. public is a visibility modifier

class Third{}  // By default this is public
private class Fourth{}
class Fifth{
    val third = Third() // No issues
    val fourth = Fourth() // Can't access because Fourth is private
}
Prakash
  • 7,794
  • 4
  • 48
  • 44
2

All classes, methods, and members are public by default BUT not open

Keyword open in kotlin means "Open for Extension"

means if you want any class to be inherited by any subclass or method to be overriden in subclasses you have to mark as open otherwise you will get compile time error

NOTE: abstract classes or methods are open by default you do not need to add explicitly.

Amir Raza
  • 2,320
  • 1
  • 23
  • 32
2

OPEN VS FINAL VS PUBLIC

OPEN :

  1. child class can access this because they are inherited by its parent.
  2. In Kotlin you need to add 'open' keyword unlike java whose all classes are 'open' by default Example :
  1. Kotlin : open class A () {}
  2. Java : class A () {}

FINAL :

  1. child class can't access or inherit.
  2. In JAVA you need to add 'final' keyword unlike kotlin whose all classes are 'final' by default Example :
  1. Kotlin : class A () {}
  2. Java : final class A () {}

PUBLIC : Any class whether its inherited or not can access its data or methods.

Example in Kotlin :

//Final
class DemoA() {
    
    protected fun Method() {

    }

}

class DemoB() : DemoA {
    Method() // can't access
}

//OPEN
open class DemoA() {
    
    protected fun Method() {

    }

}

class DemoB() : DemoA {
    Method() // can access
}

//Public
class DemoA() {

    fun Method() {

    }

}

class DemoB()  {
    val a = DemoA()
    a.Method() // can access
}

Example in Java :

//FINAL
final class DemoA() {
    protected void name() {
        
    }
}

class DemoB() extends DemoA {
    name(); // Can't access
}

//Open
class DemoA() {
    protected void name() {
        
    }
}

class DemoB() extends DemoA {
    name(); // Can access
}

//Public
class DemoA() {
    void name() {
        
    }
}

class DemoB(){
    DemoA a = new DemoA()
    a.name(); // Can access
}
0

Summarized answer (Kotlin)

  • The defaults of declarations of classes, methods, and properties are (public + final). final prevents any inheritance attempts.

  • In order to be able to extend a class, you must mark the parent class with the open keyword.

  • In order to be able to override the methods or properties, you must mark them in the parent class with the open keyword, in addition to marking the overriding method or parameter with the override keyword.

  • public is just encapsulation, it affects the visibility of classes/ methods. Public will make them visible everywhere.

Reference

Mena
  • 3,019
  • 1
  • 25
  • 54