Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 726
Danylo Fitel Avatar asked Mar 10 '13 20:03

Danylo Fitel


People also ask

Is it okay to call abstract method from constructor?

Question: Can you call an abstract method from an abstract class constructor? Answer: Yes.

Why is it not acceptable to call a virtual method from the constructor of an abstract class?

Normally calling virtual functions from constructors is considered bad practice, because overridden functions in sub-objects will not be called as the objects have not been constructed yet.

Can abstract methods have constructor in Java?

An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the super keyword.

Which of the following is false about abstract classes in Java?

Which of the following is false about abstract classes in Java? If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using 'abstract' keyword. Abstract classes can have constructors.


1 Answers

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.

like image 80
Ryan Stewart Avatar answered Sep 19 '22 03:09

Ryan Stewart