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.


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

Results 1 to 3 of 3

Thread: covariant return type

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Thanked 0 Times in 0 Posts

    Default covariant return type

    public class MyMainClass {
    public static void main (String[] args) {
    MySuperClass msup=new MySuperClass();
    MySubClass msub=new MySubClass();
    //System.out.println(msup.a +" "+ msub.a);
    WithMethod wm=new WithMethod();
    MySuperClass msc=new MySuperClass();
    System.out.println("Returned value : " + msc.a);

    class MySuperClass {
    int a;

    class MySubClass {
    int a;

    class WithMethod {
    public MySuperClass theMethod(MySubClass mscl) {
    return mscl;

    Fails with the error :

    C:\Documents and Settings\Administrator\Desktop\Java Programs>javac MyMainClass.
    MyMainClass.java:26: incompatible types
    found : MySubClass
    required: MySuperClass
    return mscl;
    1 error

    why is covariant return not working ???

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: covariant return type

    I've moved this thread to a more appropriate forum. For more information, read this: The Member Introductions Forum is for saying hi, NOT for technical questions!

    Also, when posting code, please remember to use the highlight tags.
    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,614 Times in 2,564 Posts

    Default Re: covariant return type

    incompatible types
    found : MySubClass
    required: MySuperClass
    The compiler's error message explains the problem.
    Do you understand about data types? The compiler will NOT allow you to use a variable of the wrong type.
    What type does the method's definition say that the method returns?
    What type is the mscl variable?
    Are they the same or compatible? The compiler does not think they are.

Similar Threads

  1. abstract class & 'covariant' return type?
    By jezza10181 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2011, 02:26 PM
  2. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  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. 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