1

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 ?

1
  • Please check the answer I put and if it helped mark it as a correct... Commented Jun 28, 2021 at 11:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.