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

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

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

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.

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

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.

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
}

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.

OPEN VS FINAL VS PUBLIC
OPEN :
child class can access this because they are inherited by its parent.
In Kotlin you need to add 'open' keyword unlike java whose all classes are 'open' by default
Example :
Kotlin : open class A () {}
Java : class A () {}
FINAL :
child class can't access or inherit.
In JAVA you need to add 'final' keyword unlike kotlin whose all classes are 'final' by default
Example :
Kotlin : class A () {}
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
}

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

Related

Kotlin's "internal" keyword Java interop

I'm trying to figure out what happens with internal classes when seen from Java's perspective.
Found this in the docs:
Members of internal classes go through name mangling, to make it harder to accidentally use them from Java and to allow overloading for members with the same signature that don’t see each other according to Kotlin rules
So I was very curious to see how it looks like in practice.
I created a simple Kotlin class:
internal class Foo(i : Int) {}
Built a project, unpacked the jar and used javap to have a look at the actual class... and it displayed a standard public class with the original name:
Compiled from "Foo.kt"
public final class Foo {
public Foo(int);
}
Am I missing something? or is it just the docs that are misleading?
Docs mention members of internal classes, but I tried that as well:
internal class Foo(someInt : Int) {
var someString : String
get() {
TODO()
}
set(value) {}
fun foo() { }
class Bar { }
}
And got the expected output:
Compiled from "Foo.kt"
public final class Foo {
public Foo(int);
public final java.lang.String getSomeString();
public final void setSomeString(java.lang.String);
public final void foo();
}
and:
Compiled from "Foo.kt"
public final class Foo$Bar {
public Foo$Bar();
}

Implement optional functions in an abstract class

In Kotlin I have an abstract class that other classes can inherit from. I would like to have some functions that the class that inherits this class can optionally implement. In the code below, the function is protected abstract. This however requires that the class that is inheriting this class MUST implement these functions. Is there a way to make it so that the class that is inheriting can choose to implement the functions or not implement them?
abstract class BaseDialogFragment {
protected abstract fun getButton1Text(): String
protected abstract fun getButton2Text(): String
}
It is very simple, you just provide the default implementation like in the example below and your inheritors can override them:
abstract class BaseDialogFragment {
open fun getButton1Text(): String {
TODO("Your default implementation here")
}
open fun getButton2Text(): String {
TODO("Your default implementation here")
}
}

Kotlin : Interface with immutable property implemented by class with mutable

I have the following code:
Works around a limitation in Spring where #ConfigurationProperties classes need to follow the JavaBeans convention with mutable properties.
Instead of inject the mutable TokenConfigurationConfig we inject the interface.
public interface TokenAuthenticationConfig {
public fun apiKey() : String
}
#Component
#ConfigurationProperties(prefix = "service.api")
public open class TokenAuthenticationConfigImpl : TokenAuthenticationConfig
{
public var apiKey : String
constructor() {
this.apiKey = ""
}
override fun apiKey(): String
{
return this.apiKey
}
}
It works ok, but just wondering:
Is it possible in Kotlin to define an interface with an immutable property, that is implemented by class with a mutable property.
Uses of the interface would see the property as immutable, while users of the class would see it as mutable.
Yes, it definitely is possible to define such an interface and a class.
Any (publicly visible) property x in Kotlin means a pair of methods getX() and setX(..), generated by the compiler to satisfy the Java convention. That said, it is consistent that you can override getX in a class and add setX.
Here is an example:
interface SomethingImmutable {
val Somevar: String
}
class MyClass: SomethingImmutable {
override var Somevar: String = "Initial Value"
}

Accessible from only one class

I have a class and a method in it. The method's access modifier is now private but it can be changed. Now i just want the method to be seen only one another class.
the other class and my class are in same directory by the way.
The only way to allow a method in a class to be available to only one other class is to use a nested private class.
public class Enclosing
{
private class InnerClass
{
public void MyMethodThatCanOnlyBeUsedByEnclosingClass()
{}
}
}

Base class and derived class

I have a question, I have a base class and an another class which derived from the base class. Can we access derived class in the base class.
Thanks in advance
You can access the code in the derived class from the base class code, but only from within an object which is actually a derived class object, and then only if the methods involved are virtual methods.
If you have an object which is itself an instance of the base class, then from within that instance you cannot see derived class code from the base class .
example
public class Baseclass
{
public void Foo()
{
Bar();
}
public virtual void Bar()
{
print("I'm a BaseClass");
}
}
public classs Derived: BaseClass
{
public override void Bar()
{
print("I'm a Derived Class");
}
}
Main()
{
var b = new BaseClass();
x.Foo() // prints "I'm a BaseClass"
// This Foo() calls Bar() in base class
var d = new Derived();
d.Foo() // prints "I'm a Derived Class"
// in above, the code for Foo() (in BaseClass)
// is accessing Bar() in derived class
}
No you can not. If you happen to know the an object declared as the Base class is actually the derived class, you can cast it. But within the base class you can not access the derived class's members.
There are a lot of ways that a base class can access members of a derived class (depending on programming language), but generally it is considered a design smell.
Instead, you usually want the base class to only directly access its own members, and allow derived classes to override methods.