2

Suppose i have 2 interface which is

interface add{
    
      void add2No(float a, float b);
}

and

interface Minus{
        
      void Minus2No(float a, float b);
}

then on the main method, i already overide the method which is

public class Count implements Add, Minus {
        public static void main(String[] args) throws IOException{
    
            //declare var
            float a, b;
            String temp;
    
            //create object
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Count obj = new Count();
    
            //User Input
            System.out.print("Enter your first Number : ");
            temp = br.readLine();
            a = Float.parseFloat(temp);
    
            System.out.print("Enter your second Number : ");
            temp = br.readLine();
            b = Float.parseFloat(temp);
    
      
    
            System.out.println("Value of " +a+ " + " +b+ " is : " +obj.Totaladd(float a, float b));
            System.out.println("Value of " +a+ " + " +b+ " is : " +obj.TotalMinus(float a, float b));
    
    
        }
    
        @Override
        public void Add2No(float a, float b) {
    
            float TotalAdd = a + b;
    
        }
    
        @Override
        public void Minus2No(float a, float b) {
    
            float TotalMinus = a - b;
        }
    }

Am i using the correct implementation for interface? why there's error when i try to print out the TotalAdd and TotalMinus?

1
  • It's always a good idea to paste the exact error. This is especially true in the event you have multiple problems with your code, Commented Dec 10, 2021 at 1:27

2 Answers 2

3

Yes. Because you don't return the results. Currently both methods are void. You could change that. Like,

interface Add {   
    float add2No(float a, float b);
}

interface Minus {
    float minus2No(float a, float b);
}

And then

@Override
public float add2No(float a, float b) {
    return a + b;
}

@Override
public float minus2No(float a, float b) {
    return a - b;
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are three wrong places.

The first wrong place:

Because Java is case sensitive.

The name of your interface method is called add2No, but the name of your implementation is called Add2No

The second wrong place:

There is a problem with your method parameter passing and the way of calling. I did not see the Totaladd and Totaladd methods defined in your Count object.

If you adjust the case, there should only be add2No and Minus2No methods in Count object.

You need to adjust the name of one of them, and do not pass the type when passing parameters.

For example: obj.Totaladd(float a, float b) should be obj.Totaladd(a, b)

The third wrong place:

If you need to call the method to get the value for calculation, you must adjust the type of the method, it should not be void

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.