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 2 of 2

Thread: The implementation of a function with generic return type, returns a specific class. Is this a good practice?

  1. #1
    Junior Member
    Join Date
    Mar 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The implementation of a function with generic return type, returns a specific class. Is this a good practice?

    Consider the following Class structure
    public interface Base {
    }
     
    public class C1 implements Base {
    }
     
     
    public interface Repo {
        <T extends Base> T get();
    }
     
    public class C1Repo implements Repo{
        @Override
        public C1 get() {
            return new C1();
        }
    }
    Repo interface has a function that returns <T extends Base>, but my implementation returns a specific C1 class.
    IntelliJ warns "Unchecked overriding: return type requires unchecked conversion. Found 'org.example.C1', required 'T' ". Is this okay? If not how would I restructure this composition?

  2. #2
    Junior Member
    Join Date
    Jan 2024
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The implementation of a function with generic return type, returns a specific class. Is this a good practice?

    The warning from IntelliJ suggests that your code doesn't strictly follow the contract of the Repo interface. While the interface expects a generic return type, your implementation returns a specific class, C1. To fix this, you can modify your implementation to accept a generic return type and return an object of that type. Here's how:
    public class C1Repo implements Repo {
    @Override
    public <T extends Base> T get() {
    // Return an object that extends Base
    return (T) new C1(); // Casting to T
    }
    }


    If you're finding Java assignments or projects challenging, consider seeking help from https://www.programminghomeworkhelp....va-assignment/. They have experts who can explain concepts clearly and guide you through difficult tasks step by step. Getting assistance can save you time and reduce stress, helping you improve your understanding and grades. Don't hesitate to reach out for support when you need it.

Similar Threads

  1. Replies: 3
    Last Post: April 25th, 2024, 05:44 AM
  2. Any good websites to practice programming?
    By SauronWatchesYou in forum The Cafe
    Replies: 1
    Last Post: April 17th, 2014, 08:06 AM
  3. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  4. Good examples for exam practice?
    By Twoacross in forum Java Theory & Questions
    Replies: 2
    Last Post: December 28th, 2011, 01:48 AM
  5. abstract class & 'covariant' return type?
    By jezza10181 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2011, 02:26 PM