Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these correctly declares a Thread subclass with a name fixed at construction?

  • A.class Worker extends Thread { Worker(String n) { super.setName(); } }
  • B.class Worker extends Thread { Worker() { super("worker"); } }
  • C.class Worker extends Thread { Worker() { this.name = "worker"; } }
  • D.class Worker extends Thread { Worker() { Thread("worker"); } }

Answer: B

Thread has a constructor taking a name, so a subclass passes it up with super. The name field is private, a superclass constructor cannot be called like a method, and setName takes an argument. setName may also be used after construction.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.