0

I'm trying to add two numbers using multithreading but its showing the following error on compiling :
" constructor Add in class Add cannot be applied to gives types;
class Input extends Add{
required: int,int
found: no arguments
reason: actual and formal arguments lists differ in length

import java.util.Scanner;

class Add implements Runnable{
    Thread t;
    int a,b,c;
    Add(int a,int b){
        this.a=a;
        this.b=b;
        t = new Thread(this,"add");
        t.start();
    }
    public void run(){
        c=a+b;
        System.out.println("Exiting add thread.");
    }
}
class Input extends Add{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        Add o = new Add(5,4);
        System.out.println("Enter a string: ");
        String str = sc.nextLine();
        System.out.println("String is : " + str);
        System.out.println("c: " + o.c);
    }
}
Arpit Tomar
  • 187
  • 1
  • 8
  • class Input extends Add{ //add written by mistake. – Arpit Tomar Jun 18 '15 at 19:03
  • There's an [edit](http://stackoverflow.com/posts/30923667/edit) link below the content of your question. You can select it and, uhm, **edit** the content of the question by yourself rather than posting a comment with a *fix*. – Luiggi Mendoza Jun 18 '15 at 19:06
  • Related: http://stackoverflow.com/q/25629804/1065197 http://stackoverflow.com/q/1644317/1065197 http://stackoverflow.com/q/15721764/1065197 and more and more... – Luiggi Mendoza Jun 18 '15 at 19:08
  • I think [this one](http://stackoverflow.com/q/525548/1065197) is a better dup. – Luiggi Mendoza Jun 18 '15 at 19:09
  • @LuiggiMendoza I don't think that your duplicate is correct. The solution here is to remove the inheritance, not to provide an explicit constructor that explicitly calls the superclass constructor. – rgettman Jun 18 '15 at 19:12
  • @rgettman OP's question is why the compiler raises an error and those questions explain it. After understanding this, there are two solutions: 1) remove the `extends` as you explain in your post, or 2) let `Input` extend `Add` class and provide a proper constructor for the inheritance. Which one to use will depend on what OP exactly needs, but that we don't know. – Luiggi Mendoza Jun 18 '15 at 19:13

1 Answers1

0

Since the only constructor available in Add takes two parameters, you must have a constructor in Input that explicitly calls it, but you don't. The default, implicit constructor attempts to call the superclass constructor Add(), which doesn't exist, hence the error.

But you have your Input class extending Add for no reason. Remove extends Add and it should compile.

class Input {
rgettman
  • 176,041
  • 30
  • 275
  • 357