Java: cannot use constructors in abstract class

I created the following abstract class for Task Scheduler in red5:

package com.demogames.jobs;

import com.demogames.demofacebook.MysqlDb;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.scheduling.IScheduledJob;
import org.red5.server.api.so.ISharedObject;
import org.apache.log4j.Logger;
import org.red5.server.api.Red5;

/**
 *
 * @author ufk
 */
abstract public class DemoJob implements IScheduledJob {

protected IConnection conn;
protected IClient client;
protected ISharedObject so;
protected IScope scope;
protected MysqlDb mysqldb;

protected static org.apache.log4j.Logger log = Logger
    .getLogger(DemoJob.class);

protected DemoJob (ISharedObject so, MysqlDb mysqldb){

       this.conn=Red5.getConnectionLocal();
       this.client = conn.getClient();
       this.so=so;
       this.mysqldb=mysqldb;
       this.scope=conn.getScope();
 }

 protected DemoJob(ISharedObject so) {
   this.conn=Red5.getConnectionLocal();
   this.client=this.conn.getClient();
   this.so=so;
   this.scope=conn.getScope();
 }

 protected DemoJob() {
   this.conn=Red5.getConnectionLocal();
   this.client=this.conn.getClient();
   this.scope=conn.getScope();
 }

}

      

Then I created a simple class that extends the previous one:

public class StartChallengeJob extends DemoJob {

 public void execute(ISchedulingService service) {

   log.error("test");

 }

}

      

The problem is that my main application can only see the constructor without any parameters. with the means I can do new StartChallengeJob()

why is the main application not seeing all the constructors?

thanks!

+2


a source to share


2 answers


Constructors are not inherited. StartChallengeJob looks like this:

public class StartChallengeJob extends DemoJob {

  public StartChallengeJob() {
    super();
  }

  public void execute(ISchedulingService service) {    
    log.error("test");   
  }    
}

      



If you want all superclass constructor signatures to be available, you also need to have these constructors in StartChallengeJob

:

public class StartChallengeJob extends DemoJob {

  public DemoJob (ISharedObject so, MysqlDb mysqldb) {
    super(so, mysqldb);
  }

  public DemoJob (ISharedObject so) {
    super(so);
  }

  public StartChallengeJob() {
    super();
  }

  public void execute(ISchedulingService service) {    
    log.error("test");   
  }    
}

      

+8


a source


On the class StartChallengeJob

referenced in your question, the compiler generates a default constructor that implicitly calls the base class's default constructor.

If this default behavior is not what you want, you need to explicitly define one or more constructors in StartChallengeJob

, which then call the required base class constructor. For instance. if you want both a default constructor and a parameterized constructor, you need to define both:



public class StartChallengeJob extends DemoJob {

 public StartChallengeJob(){
       // implicitly calls the base class default constructor: super();
 }

 public StartChallengeJob (ISharedObject so, MysqlDb mysqldb){
       super(so, mysqldb);
 }

 public void execute(ISchedulingService service) {

   log.error("test");

 }

}

      

+4


a source







All Articles