0

I have this thread class:

class tCallTime implements Runnable {
  private Thread t;
  private String threadName;
  public tCallTime(String name) {
    threadName = name;
    println("Creating " +  threadName );
  }
  tCallTime() {
  
  }
  void codeToRun() {
    //Override This
    callTime();
  }

  public void run() {
    println("Running " +  threadName );
    try {
      codeToRun();
      Thread.sleep(0);
    } 
    catch (InterruptedException e) {
      println("Thread " +  threadName + " interrupted.");
    }
  }

  public void start () {
    if (t == null) {
      t = new Thread (this, threadName);
      t.setPriority(10);
      println("Started " + threadName +  " with priority " + t.getPriority());
      t.start ();
    }

I tried to inherit from this by doing this:

class tCalcVertex extends tCallTime{
  @Override
  void codeToRun(){
  
    print("test");
  }
}

I then try to run it by using the following code:

  tCallTime thread = new tCallTime("Thread-1");
  thread.start();
  tCalcVertex thread2 = new tCalcVertex("Tread-2");
  thread2.start();

The compiler then tells me that "The constructor "tCalcVertex(String)" does not exist" How would i go about inheriting from this class without having to rewrite the whole class

Nixcc
  • 63
  • 7

1 Answers1

0

Well, the compiler is correct, there is no constructor in your class tCalcVertex which takes a string. There is only one in its parent class. Constructors are not automatically inherited, you have to define them explicitly for each class in the hierarchy:

class tCalcVertex extends tCallTime {
  public tCalcVertex(String name) {
    super(name);
  }

  @Override
  void codeToRun() {
    print("test");
  }
}

PS Java naming conventions name classes in PascalCase with a uppercase letter as first character. Adhering to this convention makes it a lot easier for fellow programmers to understand your code quickly.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks for the comment about PascalCase used to c++ naming conventions so this is a nice bit of information – Nixcc Aug 17 '20 at 19:54