0

My problem is with a Polymorphic method , getSalary (which should be of type double. The problem is that the salary in this case depends on the average mark of a student and thus I felt the need for an If Statement because of the 2 conditions.
However java does not seem to allow 2 returns in the same method and considers the following code as not having a return statement.

    @Override
    double getSalary()
    {
        int avg=average(); //average is just a method which returns an average of an array of numbers

        if (avg>=50) 

            return 150.00;

        else if (avg<=50) 

            return 80.00;

    }

Thanks in advance for any help and hints in how I can implement this.

2
Contributors
1
Reply
13
Views
4 Years
Discussion Span
Last Post by deceptikon
3

However java does not seem to allow 2 returns in the same method

That's not correct.

and considers the following code as not having a return statement.

This is correct. Not all paths end in a return statement, so your compiler is complaining. You have a return for the if clause, one for the else if clause, but no return for the implicit else clause. It doesn't matter that the implicit else clause is logically impossible, your compiler isn't checking the conditions, only the paths.

In this case you can turn the else if into a simple else to fix the problem, because that wouldn't break your logic.

Edited by deceptikon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.