Relationship between kotlin and java access modifiers

Introduction

We investigated how kotlin's public, ʻinternal, protected, private and java's public, package private, protected, private` look like each other.

First, let's see what kind of access rights they have.

Java access modifier (declaration for class)

public Accessable from anywhere
protected Can't declare
package private Can be accessed from the same package
private Can't declare

Java access modifier (declaration for class members)

public Accessable from all classes
protected Can be accessed from subclasses or classes in the same package
package private Can be accessed from classes in the same package
private Only accessible from the declared class

Kotlin access modifiers (declarations for classes / top-level functions / variables)

public Accessable from anywhere
internal Can be accessed from within the same module
protected Can't declare
private Only accessible from the declared class

Kotlin access modifier (declaration for class members)

public Accessable from all classes
internal Can be accessed from classes in the same module
protected Only accessible from subclasses
private Only accessible from the declared file

Relationship between access modifiers in kotlin and java

Let's get into the main subject from here, let's see what happens when kotlin access modifiers are viewed from Java.

kotlin's private class

Kotlin's private classes are considered package private in java.

A.kt


package com.example.model

private class A(name: String, age: Int, height: Int)

B.java


package com.example.model

public class B {

  public B() {
    A a = new A("taku", 23, 168); // OK
  }
}

C.java


package com.example.ui


public class C {

  public C(){
    A a = new A("taku", 23, 168) // ERROR
  }
}

kotlin internal modifier

Java does not have an internal modifier. Therefore, anything marked internal in kotlin is considered public by Java. However, if you are a class member, it will be obfuscated.

A.kt


package com.example.model

internal class A(val name: String, internal val age: Int, private val height: Int)

B.java


package com.example.model

public class B {

  public B() {
    A a = new A("taku", 23, 168); // OK

    a.getName(); // OK

    a.getAge(); // ERROR
    
    a.getAge$production_sources_for_module_example(); // OK
    
    a.getHeight(); // ERROR
  }
}

Recommended Posts

Relationship between kotlin and java access modifiers
Differences between "beginner" Java and Kotlin
[Java] Relationship between H2DB and JDBC
Conversion between Kotlin nullable and Java Optional
[For beginners] Difference between Java and Kotlin
About Java access modifiers
[Java] Difference between == and equals
Relationship between Controller and View
Relationship between package and class
[Java] Difference between Hashmap and HashTable
Relationship between ActiveRecord with_lock and cache
Relationship between database and model (basic)
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
Differences between Java and .NET Framework
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
The relationship between strict Java date checking and daylight savings time
About the relationship between the Java String equality operator (==) and initialization. Beginners
Difference between final and Immutable in Java
[Java] Differences between instance variables and class variables
A Java engineer compared Swift, Kotlin, and Java.
[Note] Cooperation between Java and DB (basic)
Relationship between Eclipse m2e plugin and Maven
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
HashMap # putAll () behaves differently between Java 7 and Java 8
Interpret the relationship between Java methods and arguments into a biochemical formula
Access modifier [Java]
[Java] Understand the difference between List and Set
Kotlin functions and lambdas to send to Java developers
Java switch statement and break, Kotlin when expression ...
Difference between next () and nextLine () in Java Scanner
Summarize the differences between C # and Java writing
Java and JavaScript
XXE and Java
Distinguish between positive and negative numbers in Java
[Java] Difference between "final variable" and "immutable object"
How to access Java Private methods and fields
Relationship between UI test and recording, implementation method
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
About access modifiers
Going back to the beginning and getting started with Java ① Data types and access modifiers
Sample (Java) for OAuth 2.0 authentication and access token acquisition
Please note the division (division) of java kotlin Int and Int
Java language from the perspective of Kotlin and C #
Write a class in Kotlin and call it in Java
Access modifier summary-JVM language edition (Java / Groovy / Scala / Kotlin)
About the relationship between HTTP methods, actions and CRUD
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
Verification of the relationship between Docker images and containers
I want to transition screens with kotlin and java!
Mutual conversion between Java objects and JSON using Moshi
[Java] [Kotlin] Generically call valueOf and values of Enum
About synchronized and Reentrant Lock (Java & Kotlin implementation example)
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
Getters and setters (Java)