Rudra Raina
Rudra Raina

Reputation: 303

Issue in count down timer class in Kotlin ( Android)

The count down timer class in Kotlin / Java is an abstract class hence we can't create an instance of it , but while viewing a tutorial of count down timer in Kotlin , this code went straight over my head

private var restTimer : CountDownTimer ? = null 

restTimer = object:CountDownTimer(10000,1000){

    
    override fun onTick(millisUntilFinished: Long) {
        // some code
    }

    override fun onFinish() {
        // some code
}.start()

Are we creating an object of this abstract class and why is "object" keyword mentioned here ?

Upvotes: 1

Views: 734

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

I recommend you to look at Object expressions and declarations.

If you check the CountDownTimer class is :

public abstract class CountDownTimer {

    ....

    /**
     * @param millisInFuture The number of millis in the future from the call
     *   to {@link #start()} until the countdown is done and {@link #onFinish()}
     *   is called.
     * @param countDownInterval The interval along the way to receive
     *   {@link #onTick(long)} callbacks.
     */
    public CountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

So it is using this constructor to create an anonymous implementation. It is not creating an instance but object can access members, methods without create an instance.

This is what you do in Java

CountDownTimer countDownTimer = new CountDownTimer(long duration, long interval) {
    @Override
    public void onTick(long millisUntilFinished) {
    }

    @Override
    public void onFinish() {
    }
};
countDownTimer.start();

So what you are doing is create an anonymous class and implement the necessary methods.

Upvotes: 1

Nitin Prakash
Nitin Prakash

Reputation: 977

Object Expressions

Object expressions create objects of anonymous classes, that is, classes that aren't explicitly declared with the class declaration. Such classes are handy for one-time use. You can define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes are also called anonymous objects because they are defined by an expression, not a name.

Object Declarations

Singleton can be useful in several cases, and Kotlin (after Scala) makes it easy to declare singletons. This is called an object declaration, and it always has a name following the object keyword. Just like a variable declaration, an object declaration is not an expression, and cannot be used on the right-hand side of an assignment statement.

Object declaration's initialization is thread-safe and done at first access

Like yours:

    restTimer = object:CountDownTimer(10000,1000){

    
    override fun onTick(millisUntilFinished: Long) {
        // some code
    }

    override fun onFinish() {
        // some code
}.start()

source: https://kotlinlang.org/docs/object-declarations.html#object-declarations

Upvotes: 0

Related Questions