Java classes in Kotlin 

Let's explore how to create a Java class object in Kotlin. To do this, create a Shape.java file with three properties: height, width, and name with getters and setters:

public class Shape {

private int width;
private int height;
public static final double PI = 3.1415;
private final String name;

public Shape(int width, int height, String name) {
this.width = width;
this.height = height;
this.name = name;
}
public final int getHeight() {
return this.height;
}

public final void setHeight(int value) {
this.height = value;
}

public final String getName() {
return this.name;
}
public final void shapeMessage() {
System.out.println("Hi i am " + this.name + ", how are you doing");
}
}

Creating an instance of the Java class in Kotlin is similar to creating an instance of the Kotlin class. See the following example:

val shape = Shape(5,10,"Square")

shape is an instance of the Shape class, which can access functions and update the class properties:

shape.shapeMessage()
shape.height = 10
println("name ${shape.name} height = ${shape.height}")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.239.41