What is the difference between 'open' and 'public' in Kotlin?

后端 未结 6 1885
面向向阳花
面向向阳花 2021-01-03 19:26

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

相关标签:
6条回答
  • 2021-01-03 19:56

    The open keyword means “open for extension“:

    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 etc. 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

    0 讨论(0)
  • 2021-01-03 19:56

    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
    }
    
    0 讨论(0)
  • 2021-01-03 19:57

    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

    0 讨论(0)
  • 2021-01-03 20:00

    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.

    0 讨论(0)
  • 2021-01-03 20:05

    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.

    0 讨论(0)
  • 2021-01-03 20:14

    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.

    0 讨论(0)
提交回复
热议问题