James Mitchell
James Mitchell

Reputation: 2467

Multiple getter methods for different amount of parameters

I know you can have multiple constructors, for example:

public Game (int num, boolean truth) {

}

public Game (int num) {
    this(num, false);
}

Can the same be done for getter methods? So I can have two parameters, but if the user only wants to use one, the other will automatically be filled in. Such as

public int wins (int num, boolean truth) {
    return num*2;
}

public int wins (int num) {
    this(num, false);
}

Upvotes: 0

Views: 121

Answers (2)

agold
agold

Reputation: 6276

Yes you can do this, this is called overloading:

public class DataArtist {
    ...
    public void draw(String s) {
        ...
    }
    public void draw(int i) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

Upvotes: 1

Salah
Salah

Reputation: 8657

You can declare a delegate methods like:

public int wins (int num, boolean truth) {
    return num * 2;
}

public int wins (int num) {
    return wins(num, false);
}

In this case this keyword is use only to call constructors.

Upvotes: 1

Related Questions