I'm been working through my textbook and just can't seem to understand the section on covariant return types. ie) what are covariant return types, what they are used for, etc. The definition in the book is as follows.

An overridden method in a derived class can return a type derived from the type returned by the base-class method.

Any explanation, link to a simple tutorial or example would be greatly appreciated!

The example from the text. What is the author trying to get across?

//: polymorphism/CovariantReturn.java

class Grain {
  public String toString() { return "Grain"; }
}

class Wheat extends Grain {
  public String toString() { return "Wheat"; }
}

class Mill {
  Grain process() { return new Grain(); }
}

class WheatMill extends Mill {
  Wheat process() { return new Wheat(); }
}

public class CovariantReturn {
  public static void main(String[] args) {
    Mill m = new Mill();
    Grain g = m.process();
    System.out.println(g);
    m = new WheatMill();
    g = m.process();
    System.out.println(g);
  }
} /* Output:
Grain
Wheat
*///:~

Recommended Answers

All 6 Replies

All it looks like that's showing you is that an object of type wheat can be stored in a grain variable, since wheat extends grain, but the proper method will be called even though the variable is of type grain.

Calling the Mill's process() method returns an instance of type Grain.
Calling the overriden process() method of WheatMill returns an instance of type Wheat.

The compiler can recognize this like if the Wheat class had an exclusive method foo(), the following would be able to compile:

(new WheatMill()).process().foo();

and this would fail during compilation:

(new Mill()).process().foo();

Thank you, VinC, for sharing the "Covariant".
line 22 shows that g is a variable of type Grain.
line 25 shows that a variable of type Grain is capable to receive a value of type Wheat. Why?
Because Grain is super class of class Wheat. Wheat is a kind of Grain. An instance of subclass can also be an instance of superclass.
Class Student inherits class Person. Can you say: "Student David is not a person"? definitely Not. David, the instance of class Student is definitely a person.
Also see Java tips

I get the following error trying to run the source code on JDK 1.7:
error: method process in class Mill cannot be applied to given types;
g = m.process(g);
^

well ... this has not very much to do with what JDK you are using, more as what type of object you use where and which you should use where.

also, you could have created a new thread for this.

I'm been working through my textbook and just can't seem to understand the section on covariant return types. ie) what are covariant return types, what they are used for, etc. The definition in the book is as follows. Any explanation, link to a simple tutorial or example would be greatly appreciated!

The example from the text. What is the author trying to get across?

//: polymorphism/CovariantReturn.java

class Grain {
  public String toString() { return "Grain"; }
}

class Wheat extends Grain {
  public String toString() { return "Wheat"; }
}

class Mill {
  Grain process() { return new Grain(); }
}

class WheatMill extends Mill {
  Wheat process() { return new Wheat(); }
}

public class CovariantReturn {
  public static void main(String[] args) {
    Mill m = new Mill();
    Grain g = m.process();
    System.out.println(g);
    m = new WheatMill();
    g = m.process();
    System.out.println(g);
  }
} /* Output:
Grain
Wheat
*///:~

Guys this thread was from Nov 12th 2006???? im sure the problem is solved or the OP is dead :) lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.