KernelPanic
KernelPanic

Reputation: 2432

java getter overloading

Why I cannot overload class member of type JTextField in a following fashion: `

private JTextField m_oFilename=new JTextField();
    public JTextField filename()
        { return this.m_oFilename; }
    public String filename()
        { return this.m_oFilename.getText(); }

Upvotes: 1

Views: 2765

Answers (2)

rgettman
rgettman

Reputation: 178293

In Java, overloading refers to methods of the same name that have different method signatures. However, the return type is not part of the method signature.

The compiler must know which return type to resolve, and if the method signatures are the same, then it can't tell them apart or know which to use, so this is disallowed, per JLS 8.4.2.

Upvotes: 5

Thomas Sidoti
Thomas Sidoti

Reputation: 193

Because it doesn't use the output of a method to determine which method it should call.

For example, which method should run if you just executed:

m_oFilename.filename();

The compiler wouldn't know which to run, so this can't compile.

Upvotes: 2

Related Questions