Classes and Objects in Kotlin
1)Class :
Classes in Kotlin are declared using the keyword class{: .keyword }:
class Address { }
The class declaration consists of :
· Class name.
· Class header.(optional)
· Class body (surrounded by curly braces).(oprtional)
Both the header and the body are optional; if the class has no body, curly braces can be omitted.
class Address
2)Constructors :
Kotlin have two types of constructors.
1)Primary constructor :
The primary constructor is part of the class header: it is used after the class name.
class Place constructor(firstName: String) { }
constructor keyword can be omitted if constructor does not have any annotations or visibility modifiers.
class Place(placeName: String) { }
Primary constructor cannot contain any code and Initialization code can be placed in initializer blocks.
class Employee(name: String) {
init {
logger.info("Employee initialized with value ${name}")
}
}
I
f the constructor has annotations or visibility modifiers, the constructor{: .keyword } keyword is required, and the modifiers go before it:
class Employee public @Inject constructor(name: String) { ... }
Classes and Objects in Kotlin
1)Class :
2)Secondary constructor :
Used inside class which are prefixed with constructor{: .keyword }:
class Employee {
constructor(parent: Employee)
{
parent.children.add(this)
}
}
If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this{: .keyword } keyword:
class Employee(val name: String) {
constructor(name: String, parent: Employee) : this(name)
{
parent.children.add(this)
}
}
3)Creating instances or Objects of classes:
Kotlin doesn not have “new” keyword for creating object. Objects are created as shown below:
val employee = Employee()
val student = Student("Joe Smith")
Some examples on class and objects :
Example1 :
Java code :
class Student{
int id;//field or data member or instance variable
String name;
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Kotlin Code :
class Student {
var id:Int = 0//field or data member or instance variable
var name:String
companion object {
@JvmStatic fun main(args:Array<String>) {
val s1 = Student()//creating an object of Student
println(s1.id)//accessing member through reference variable
println(s1.name)
}
}
}
Example 2 :
Java code :
var id:Int = 0//field or data member or instance variable
var name:String
companion object {
@JvmStatic fun main(args:Array<String>) {
val s1 = Student()//creating an object of Student
println(s1.id)//accessing member through reference variable
println(s1.name)
}
}
}
Example 2 :
Java code :
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){System.out.println(length*width);}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Kotlin Code :
class Rectangle {
var length:Int = 0
var width:Int = 0
fun insert(l:Int, w:Int) {
length = l
width = w
}
fun calculateArea() {
println(length * width)
}
}
internal object TestRectangle1 {
@JvmStatic fun main(args:Array<String>) {
val r1 = Rectangle()
val r2 = Rectangle()
r1.insert(11, 5)
r2.insert(3, 15)
r1.calculateArea()
r2.calculateArea()
}
}
No comments:
Post a Comment