Language syntax
Java Covariant return type
An overriding method may refine a reference return type to a subtype. The compile-time declaration still determines the type of the call expression.
Formal Syntax & Structure
java syntaxjava
@Override public Receipt create()Working Example
Example Snippetjava
class Document {}class Receipt extends Document { public String number() { return "R7"; }}class Printer { public Document create() { return new Document(); } public static String kind() { return "Printer"; }}class ReceiptPrinter extends Printer { @Override public Receipt create() { return new Receipt(); } public static String kind() { return "Receipt printer"; }}public class Main { public static void main(String[] args) { Printer printer = new ReceiptPrinter(); Document document = printer.create(); System.out.println(document instanceof Receipt); Receipt receipt = new ReceiptPrinter().create(); System.out.println(receipt.number()); System.out.println(Printer.kind()); System.out.println(ReceiptPrinter.kind()); }}