Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: abstract class & 'covariant' return type?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default abstract class & 'covariant' return type?

    Hi Forum,

    I have written an abstract class called 'Field', which within has an abstract method called 'getValue()'. So, so far I have:-

    public abstract class Field
    {
    // instance variables
    private double x;
    private double y;


    public Field(double x)
    {
    this.x = x;
    }

    public Field(double x, double y)
    {
    this.x = x;
    this.y = y;
    }


    abstract double getValue();

    }


    Now, I have two subclasses of Field, called 'Real' and 'Complex', which both have concrete implementations of the method 'getValue()'. So, they both look like:-


    public class Real extends Field
    {
    // instance variables
    private double x;

    public Real(double a)
    {
    super(a);
    }


    public double getValue()
    {
    return(this.x);
    }

    }


    And, 'Complex' looks like:-

    public class Complex extends Field
    {
    // instance variables
    private double x;
    private double y;

    public Complex(double a, double b)
    {
    super(a, b);
    }

    public double getValue()
    {
    return(0.0);
    }
    }


    Now, my problem is, is that I want the version of 'getValue()' in the Complex class to have a different return type than 'double'.Possibly for it to have either a 'void' or an error message.

    I have tried messing around with generics, and also 'covariant return types', but cannot seem to get either to work without the compiler complaining. Any ideas appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: abstract class & 'covariant' return type?

    If you want it to return something other than a double, than you don't want it to be a Field. Or if you want Fields to return something other than a double, you have to specify that. Simple as that.

    You could throw an exception before the return statement though.

    Or you could look into Double.NaN: Double (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,355
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: abstract class & 'covariant' return type?

    Looks like if you want to return something other than a Double, you should re-evaluate your inheritance methods. Generics would work - but you don't post what you say didn't work so we can't help you with that unless you do (please use the code tags). Lastly, the wrapper class Double that Kevin referred to extends Number, so you could return a Number in which case the subclasses of Field could return Integer, Double, Byte, or anything else that extends Number.

Similar Threads

  1. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 02:56 AM
  2. Abstract class
    By jeskoston in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 02:46 PM
  3. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM
  4. abstract class
    By robinglow in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2010, 02:36 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM