Updated March 31, 2023
Introduction to Kotlin anonymous class
The kotlin anonymous class is one of the features, and it is the local classes which except that they do not have the name by using object expressions we can create the objects of the anonymous class that is, the classes that have not been explicitly declared with the class also such classes are handy for one-time use we can define it from the scratch that can be inherited from the existing classes or implement with the interfaces the instance of the anonymous class are also defined as the anonymous object since it’s an expression it is not considered to be the name of the class.
Syntax of Kotlin anonymous class
In kotlin language, we used classes, sealed class, kclass, data class, etc. These are the same type of classes handled by the kotlin for each of its has its own syntax and attributes for implementing the application.
open class name()
{
---some codes depends on the requirement---
}
fun main(args:Array<String>)
{
val varname= object:name()
----some logic codes—
}
Or
interface name
{
fun names()
}
fun main(args:Arrays<String>)
{
var n:name=object:name
{
override fun names()
{
--some codes—
}
}
n.names()
}
The above codes are the basic syntax for creating the anonymous class in different ways like we create with the help of an open class name() and interface name. We can achieve the anonymous class in kotlin with the above ways.
How anonymous class Works in Kotlin?
- The anonymous class is one of the features and concepts for the kotlin language. It is used to create the class instance by using the object expression. The object declarations are one of the singleton object patterns where the class can have only one instance; it is more useful for to working the backend like databases etc. In kotlin, we can achieve this by using the “Object” keyword; the object declaration contains properties, methods, and so on. However, mainly it does not allowed the constructor to create the object.
- Like that object, a keyword is used for to create the objects of the anonymous class known as the anonymous object. They are used to create the object if it needed a slight modification of the same class or interface without declaring the subclass in the kotlin. For example, if suppose we are implementing the class with a separate constructor to declare the anonymous object, we need to pass the appropriate constructor and parameters while calling the method in the main.
- The anonymous class and object are also achieved by using the interface; we can create the interface with some methods. The interface does not have a body, so we can create the object of the interface by using the object keyword, so the anonymous object is created at the time. Then, with the help of its reference, we can call the methods.
Examples of Kotlin anonymous class
Different examples are mentioned below:
Example #1
Code:
package one;
open class Department(empname: String, id: Int) {
init {
println("empname: $empname, id: $id")
}
fun IT() = println("Welcome To My Domain and welcome to Our IT department.")
fun Accounts() = println("Welcome To My Domain and welcome to Our Accounts department.")
fun Finance() = println("Welcome To My Domain and welcome to Our Finance department.")
fun Support() = println("Welcome To My Domain and welcome to Our Support department.")
fun Management() = println("Welcome To My Domain and welcome to Our Management department.")
fun Security() = println("Welcome To My Domain and welcome to Our Security department.")
fun QA() = println("Welcome To My Domain and welcome to Our QA department.")
fun Development() = println("Welcome To My Domain and welcome to Our Development department.")
fun Document() = println("Welcome To My Domain and welcome to Our Document department.")
fun Hosting() = println("Welcome To My Domain and welcome to Our Hosting department.")
open fun HR() = println("Welcome To My Domain and welcome to Our HR department.")
}
fun main(args: Array<String>) {
val vars = object : Department("siva", 41) {
override fun HR() = println("I dont have the HR. I am an contract employee from the vendor.")
}
vars.IT()
vars.Accounts()
vars.Finance()
vars.Support()
vars.Management()
vars.Security()
vars.QA()
vars.Development()
vars.Document()
vars.Hosting()
vars.HR()
}
Output:
In the above example, we used class for to create the anonymous object in the main method. With the help of its variable reference, we can call the interface method, which is overrided.
Example #2
Code:
package one;
interface MainDetails
{
fun Empdetail1()
{
val id:Int=41
var name:String="Arun"
}
fun Empdetail2()
{
val id:Int=43
var name:String="Raman"
}
fun Empdetail3()
{
val id:Int=44
var name:String="Kumar"
}
}
fun main(args:Array<String>)
{
var n:MainDetails=object:MainDetails
{
override fun Empdetail1()
{
val id:Int=42
var name:String="Sivaraman"
println("Welcome To My Domain its the second example that related to the kotlin anonymous class $name,$id")
}
override fun Empdetail2()
{
val id:Int=45
var name:String="Arun kumar"
println("Welcome To My Domain its the second example that related to the kotlin anonymous class $name,$id")
}
override fun Empdetail3()
{
val id:Int=46
var name:String="Bala murali"
println("Welcome To My Domain its the second example that related to the kotlin anonymous class $name,$id")
}
}
n.Empdetail1()
n.Empdetail2()
n.Empdetail3()
}
Output:
In the second example, we used to interface and implement the interface with a separate class, and we can create the object by using the object keyword.
Example #3
Code:
package one;
open class Days
{
open fun Sunday()
{
println("Welcome To My Domain Its the Sunday")
}
fun Monday()
{
println("Welcome To My Domain Its the Monday")
}
fun Tuesday()
{
println("Welcome To My Domain Its the Tuesday")
}
}
fun main(args:Array<String>)
{
var n=object:Days(){
fun Wednesday()
{println("Welcome To My Domain Its the Wednesday")
}
fun Thrusday()
{println("Welcome To My Domain Its the Thrusday")
}
fun Friday()
{println("Welcome To My Domain Its the Friday")
}
fun Saturday()
{println("Welcome To My Domain Its the Saturday")
}
}
n.Wednesday()
n.Thrusday()
n.Friday()
n.Saturday()
}
Output:
In the final example, we created the open class and method used to create the anonymous class object in the kotlin.
Conclusion
In the concluded part, the anonymous class is one of the inner classes type used to implement the functions and other logic, but the class name is not mentioned on the code. By using the object keyword to create the object, we can implement and override the methods from both class and interfaces.
Recommended Articles
This is a guide to Kotlin anonymous class. Here we discuss the introduction, syntax, and working of anonymous class in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –