Object Expression in Kotlin

This post was originally posted at https://agrawalsuneet.github.io/blogs/object-expression-in-kotlin/ and reposted on Medium on 6th Oct 2018.
public class AClass {    public void someFunction(){
System.out.println("SomeClass : some Function");
}
}
public class UsageClass {

public void useSomeClass(AClass someClassObj){
someClassObj.someFunction();
}
}
UsageClass usageClass = new UsageClass();usageClass.useSomeClass(new AClass(){
@Override
public void someFunction() {
super.someFunction();
System.out.println("UsageClass : overridden some Function");
}
});
open class AClass {
open fun someFunction() {
println("SomeClass : some Function")
}
}
class UsageClass {

fun useSomeClass(someClassObj: AClass) {
someClassObj.someFunction()
}
}
val usageClass = UsageClass()usageClass.useSomeClass(object : AClass() {
override fun someFunction() {
super.someFunction()
println("UsageClass : overridden some Function")
}
})
public abstract class AAbstractClass {
abstract void doSomething();

public void iAmDoingSomething() {
System.out.println("AAbstractClass : I am doing something");
}
}

public interface IInterface {
void canIDoSomething();
}
public class UsageClass {

public void useAbstractClass(AAbstractClass abstractClassObj) {

abstractClassObj.doSomething();
abstractClassObj.iAmDoingSomething();
}

public void useInterface(IInterface iInterfaceObject) {
iInterfaceObject.canIDoSomething();
}
}
UsageClass usageClass = new UsageClass();usageClass.useAbstractClass(new AAbstractClass() {
@Override
void doSomething() {
System.out.println("UsageClass : I am doing something");
}
});

usageClass.useInterface(new IInterface() {
@Override
public void canIDoSomething() {
System.out.println("UsageClass : can I Do Something");
}
});
abstract class AAbstractClass {
internal abstract fun doSomething()

fun iAmDoingSomething() {
println("AAbstractClass : I am doing something")
}
}

interface IInterface {
fun canIDoSomething()
}
class UsageClass {

fun useAbstractClass(abstractClassObj: AAbstractClass) {

abstractClassObj.doSomething()
abstractClassObj.iAmDoingSomething()
}

fun useInterface(iInterfaceObject: IInterface) {
iInterfaceObject.canIDoSomething()
}

fun useSomeClass(someClassObj: AClass) {
someClassObj.someFunction()
}
}
val usageClass = UsageClass()usageClass.useAbstractClass(object : AAbstractClass() {
override fun doSomething() {
println("UsageClass : I am doing something")
}
})

usageClass.useInterface(object : IInterface {
override fun canIDoSomething() {
println("UsageClass : can I Do Something")
}
})
open class BClass(val x: Int){
open val y = 20
}
interface IInterface {
fun canIDoSomething()
}
val bClassObj : BClass = object : BClass(10), IInterface {

override val y: Int = 30

override fun canIDoSomething() {
println("bClassObj : can I do something")
}

}
val justAnObject = object {
var x = 10
var y = 20
fun sum(): Int = x + y
}
class CClass {
// Private function, so the return type is
// the anonymous object type
private fun privateFunction() = object {
val x: String = "x"
}

// Public function, so the return type is Any
fun publicFunction() = object {
val x: String = "x"
}

fun main() {
val x1 = privateFunction().x // Works fine
val x2 = publicFunction().x // ERROR:
//Unresolved reference 'x'
}
}
int value = 0;usageClass.useInterface(new IInterface() {
@Override
public void canIDoSomething() {
value++; // Error

// variable 'value' is access from withing inner class, needs to be declared final.
}
});
var value = 0

usageClass.useInterface(object : IInterface {
override fun canIDoSomething() {
value++ // works fine
}
})

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Road To ZIO[n]: chapter 2, Types in the Domain layer

Tutorial: Validating Data Set Matches with MTurk

GIT Good: A Practical Introduction to GIT and GitHub (In GIT we trust!)

DYNAMIC ROUTING PROTOCOL DIVIDED INTO TWO TYPES ONE IS INTERIOR GATEWAY PROTOCOL AND ANOTHER IS…

CS373 Spring 2022: Hrithik Ramganesh: Final Entry

Now 512 people will be able to join WhatsApp group together, up to 2GB file will be shared, update…

Newbie developers, meet your Build MC

4 benefits of scheduling appointments online

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

RetryWithTimes — A helper function build with coroutine

Kotlin When Expression

Null Safety in Kotlin

Extension Functions in Kotlin