Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor overloading with Kotlin

Tags:

android

kotlin

As i have one User class having 2 parameters : first_name, last_name. So my kotlin class with be :

data class User(val first_name:String, val last_name:String) 

Now i want a constructor which will accept only first_name, or you can say just one parameter. How can i define it with Kotlin?

I know we can pass default value and in that way we can ignore second parameter, but how can we write multiple constructor?

like image 638
Ravi Avatar asked May 25 '17 08:05

Ravi


People also ask

Is there overloading in Kotlin?

Kotlin supports overloading for callables and properties, that is, the ability for several callables (functions or function-like properties) or properties with the same name to coexist in the same scope, with the compiler picking the most suitable one when such entity is referenced.

How do I call a constructor in Kotlin?

In Kotlin, you can also call a constructor from another constructor of the same class (like in Java) using this() .

Does Kotlin have constructor?

Constructors. A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is a part of the class header, and it goes after the class name and optional type parameters. The primary constructor cannot contain any code.

What are the two types of constructors in Kotlin?

A Kotlin class can have following two type of constructors: Primary Constructor. Second Constructors.

How many constructors can a class have in Kotlin?

A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).

What is method overloading in Kotlin?

Kotlin | Method Overloading: Here, we are implementing a Kotlin program to demonstrate the example of method overloading. Function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Top Interview Coding Problems/Challenges!

How to implement operators in Kotlin?

Kotlin allows you to provide custom implementations for the predefined set of operators on types. These operators have predefined symbolic representation (like + or *) and precedence. To implement an operator, provide a member function or an extension function with a specific name for the corresponding type.

What is overloaded constructor?

Overloaded constructor is called based upon the parameters specified when new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading.


1 Answers

You can define extra constructors in the class body

data class User(val firstName: String, val lastName: String) {      constructor(firstName: String) : this(firstName, "")  } 

These 'secondary constructors' have to call through to the primary constructor or a different secondary constructor. See the Official documentation on constructors.

So, in effect this is the same as just a primary constructor with default argument, which would be the idiomatic way to go.

data class User(val firstName: String, val lastName: String = "") 
like image 53
RobCo Avatar answered Nov 07 '22 10:11

RobCo