Łukasz Bownik
Łukasz Bownik

Reputation: 6237

Overloading methods by return type in java

Can I collapse those two "getFields" methods into one using generics (the second method is intended to package private access) or should I rename the second one into something ugly like "getFieldPackagePrivate"?

public interface IField {

}


class Field implements IField { // package private class

}


  public class IForm {


     public List<IField> getFields();

  }



public class Form {
  private List<Field> fields;


  public List<IField> getFields() {
    return this.fields;

  }

  List<Field> getFields() { // package visible
    return this.fields;

  }

}

the basic usage would be like this

// by interfece - from client code

  IForm form;
  List<IField> list = form.getFields();

// by class - from other classes in the same package
  Form form2;
  List<Field> list2 = forms2.getFields();

The idea is to have a clean, save interface for an outside world and a convenient access (without downcasts) from package visible implementation clacces. My Field Class has a few methods that I don't want do be seen by a client but need to invoke from other classes in the same packege (like for example setLabel etc.)

Upvotes: 2

Views: 2971

Answers (4)

setec
setec

Reputation: 16090

You can do it with bytecode manipulation, check this article: http://today.java.net/pub/a/today/2008/07/31/return-type-based-method-overloading.html

Upvotes: 0

Łukasz Bownik
Łukasz Bownik

Reputation: 6237

After I experimented a little I found a solution based on Jon Skeet's answer

The code looks like this.

public interface IField {

}


class Field implements IField { // package private class

}


  public class IForm {


     public List<? extends IField> getFields();

  }



public class Form {
  private List<Field> fields;


  List<? extends Field> getFields() { // package visible - please note "extends Field" not "extends IField"
    return this.fields;

  }

}

now the dote below works

  IForm form;
  List<IField> list = form.getFields();

// by class - from other classes in the same package
  Form form2;
  List<Field> list2 = forms2.getFields();

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

You can write (as per my answer to your previous question):

public List<? extends IField> getFields()

but you can't overload by return type, and you can't expose a package-private type in a public method.

EDIT: With the change of the question, the answer is a simple "no". You'll have to rename your other method.

Upvotes: 3

Marko
Marko

Reputation: 31375

No. You can't have polymorphism based on return type in Java.

Upvotes: 3

Related Questions