0

I just started learning Java and I have a problem regarding method-overloading.

I want to know if I can call a variation of an overloaded method inside the scope of another variation of the overloaded method in question.

My main question is: What problems could possibly arise from doing that?

For example:

public class RoomClass {

    int roomNo = 1;
    String roomType = "Lecture Hall";
    String roomArea = "First Floor";
    boolean ACMachine = true;

    protected void setData(int roomNo, String roomType, String roomArea, boolean ACMachine) {

        setData(roomNo);
        setData(roomType, roomArea);
        setData(ACMachine);
    }

    protected void setData(int roomNo) {
        this.roomNo = roomNo;
    }

    protected void setData(String roomType, String roomArea) {

        this.roomType = roomType;
        this.roomArea = roomArea;
    }

    protected void setData(boolean ACMachine) {

        this.ACMachine = ACMachine;
    }
}

The code above is about an exercise. I couldn't find an answer to my question online or in a book. What I am trying to do is to avoid repeating code, so I figured "why not call on the other methods, since they are doing exactly what I need?". So I just aggregated all the other methods into the first one.

3
  • why not just run your program and find out? Commented Apr 1, 2018 at 2:59
  • The code I was writing was about an exercise in java (beginner level). I wanted to know if this example could be viable in large projects, or if it would create problems. P.S. I did run the program before posting the question and it worked fine. Commented Apr 1, 2018 at 3:09
  • it wont arise any problem according to me... analogy - suppose you have a window in an office where it took the form with all details ( first function ) then within its office it has different people with different responsibilities ( other functions ) which it will send the data to process , it could be same designation person but with one specific task to perform ( e.g setData(int roomNo) ) or could be another person with different designation with different task... they can all call each other whenever its required... hope it helps.. Commented Apr 1, 2018 at 3:13

3 Answers 3

1

Not only is it safe, it's a very common thing to do. Java doesn't have default parameter values, so you'll often see overloads with fewer parameters calling overloads with more parameters and supplying default values. For example:

public void foo(int a, int b) {
    // do something with a and b
}

public void foo(int a) {
    // supply default value of 0 for b
    foo(a, 0);
}

One problem that can arise is if someone extends your class and overrides foo(int, int). This will also affect the behavior of foo(int), which may be unexpected. To prevent this, you can make foo(int, int) or the entire class final. If you allow extension, you should document which methods call other methods. This is a rare exception to the rule that you should not document implementation details.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no problem in doing so as programmatically it is correct.Overloaded methods can be called from another overloaded method.

Comments

0

There is no problem at all. Java Virtual Machine can identify overloaded methods separately by the number of arguments and the data types. I don't see any problem in your example.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.