Is it OK to call abstract method from constructor in Java?

Danylo Fitel :

Let's suppose I have an abstract Base class that implements Runnable interface.

public abstract class Base implements Runnable {

  protected int param;

  public Base(final int param) {
      System.out.println("Base constructor");
      this.param = param;
      // I'm using this param here
      new Thread(this).start();
      System.out.println("Derivative thread created with param " + param);
  }

  @Override
  abstract public void run();
}

And here is one of a few derivative classes.

public class Derivative extends Base {

  public Derivative(final int param) {
      super(param);
  }

  @Override
  public void run() {
      System.out.println("Derivative is running with param " + param);
  }

  public static void main(String[] args) {
      Derivative thread = new Derivative(1);
  }

}

The point is that I want my Base class do some general stuff instead of copying it every time. Actually, it's running fine, the output is always the same:

Base constructor Derivative thread created with param 1 Derivative is running with param 1

But is it safe IN JAVA to start a thread calling the abstract method in constructor? Because, in C++ and C# it is unsafe in most cases, so far as I know. Thank you!

Ryan Stewart :

This code demonstrates why you should never call an abstract method, or any other overridable method, from a constructor:

abstract class Super {
    Super() {
        doSubStuff();
    }
    abstract void doSubStuff();
}

class Sub extends Super {
    String s = "Hello world";

    void doSubStuff() {
        System.out.println(s);
    }
}

public static void main(String[] args) {
    new Sub();
}

When run, this prints null. This means the only "safe" methods to have in a constructor are private and/or final ones.

On the other hand, your code doesn't actually call an abstract method from a constructor. Instead, you pass an uninitialized object to another thread for processing, which is worse, since the thread you're starting may be given priority and execute before your Base finishes its initialization.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java call method in constructor and abstract class?

From Dev

Typescript - call abstract method from abstract constructor or provide equivalent functionality

From Dev

How to call a constructor from main method in java?

From Dev

Call static method from constructor in Java

From Dev

How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

From Dev

Abstract class constructor call overridable method

From Dev

Is it ok to call the method again from inside the method?

From Dev

Safe alternative to calling of abstract method from constructor

From Java

Why is it considered bad practice in Java to call a method from within a constructor?

From Dev

Call constructor in an abstract class

From Dev

Unable to call abstract class method from interface

From Dev

Is it possible to call child method from abstract?

From Dev

Call a method from abstract class c#

From Java

Is it valid to call an abstract method on the super class in java

From Java

How to call abstract method from abstract class called by inherit class

From Dev

Abstract method not callable in constructor

From Dev

Does Java call an abstract classe's constructor automatically?

From Java

Java abstract class, abstract constructor

From Dev

Typescript - returning the constructor of a child class from an abstract method

From Dev

Call abstract java class from jruby with a generic

From Dev

C++ Call an abstract method from a static method using *this

From Java

How to call a method from another class with no constructor

From Dev

Can we call an async method from a constructor?

From Dev

call parent method from child constructor

From Dev

How to call a method from a class that contains a constructor?

From Dev

Call Instance Method from Subclass Constructor

From Dev

Can't call method from constructor

From Dev

Call constructor in abstract class with generics

From Dev

Call an abstract method with prototype

Related Related

  1. 1

    java call method in constructor and abstract class?

  2. 2

    Typescript - call abstract method from abstract constructor or provide equivalent functionality

  3. 3

    How to call a constructor from main method in java?

  4. 4

    Call static method from constructor in Java

  5. 5

    How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

  6. 6

    Abstract class constructor call overridable method

  7. 7

    Is it ok to call the method again from inside the method?

  8. 8

    Safe alternative to calling of abstract method from constructor

  9. 9

    Why is it considered bad practice in Java to call a method from within a constructor?

  10. 10

    Call constructor in an abstract class

  11. 11

    Unable to call abstract class method from interface

  12. 12

    Is it possible to call child method from abstract?

  13. 13

    Call a method from abstract class c#

  14. 14

    Is it valid to call an abstract method on the super class in java

  15. 15

    How to call abstract method from abstract class called by inherit class

  16. 16

    Abstract method not callable in constructor

  17. 17

    Does Java call an abstract classe's constructor automatically?

  18. 18

    Java abstract class, abstract constructor

  19. 19

    Typescript - returning the constructor of a child class from an abstract method

  20. 20

    Call abstract java class from jruby with a generic

  21. 21

    C++ Call an abstract method from a static method using *this

  22. 22

    How to call a method from another class with no constructor

  23. 23

    Can we call an async method from a constructor?

  24. 24

    call parent method from child constructor

  25. 25

    How to call a method from a class that contains a constructor?

  26. 26

    Call Instance Method from Subclass Constructor

  27. 27

    Can't call method from constructor

  28. 28

    Call constructor in abstract class with generics

  29. 29

    Call an abstract method with prototype

HotTag

Archive