java - What's wrong with Add() constructor? -
this question has answer here:
- java: inherit constructor 5 answers
i'm trying add 2 numbers using multithreading showing following error on compiling :
" constructor add in class add cannot applied gives types;
class input extends add{
required: int,int
found: no arguments
reason: actual , 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 string: "); string str = sc.nextline(); system.out.println("string : " + str); system.out.println("c: " + o.c); } }
since constructor available in add takes 2 parameters, must have constructor in input explicitly calls it, don't. default, implicit constructor attempts call superclass constructor add(), doesn't exist, hence error.
but have input class extending add no reason. remove extends add , should compile.
class input {
Comments
Post a Comment