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

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
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    117
    Thanks
    1
    Thanked 3 Times in 3 Posts

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

    Thank you. Is the same as me using a ImageBam.com free host site?

    I noticed that on one site I placed a picture on they had this on my screen. ( https://thumbs4.imagebam.com/37/d7/b8/MESWM2T_t.gif ). ImageBam place this then on their site for me. I using their server.

    Is the same thing as Java.net.URL; Package? I am studying the class files now. It's not C1 type class file or what ever that is, but it's a class file they used on my screen. Shown something like this. [URL = link or url][IMG] something in here [/IMG][/URL] I believe these are class files from a package Java.net.URL. But it works. They saw a picture on their screen like I did. ImageBam place this on for me using their servers. The idea is there in Java and packages or class files. Do they allow host number 8080 or port number? This would be different association with the present URL socket I'm on. Linux. Gateway.
    Last edited by Helium c2; April 15th, 2024 at 10:49 PM. Reason: add information

  3. #3
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    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 compiler can't guarantee that C1 is compatible with T, which results in the warning

    --- Update ---

    check this https://waytojava.com/java/generics

  4. #4
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

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

    It's understandable that you're seeking clarity on this matter. The warning from IntelliJ is indicating a potential issue with type safety. Let's break down what's happening and suggest a solution.

    In your current implementation, `C1Repo` implements the `Repo` interface, which mandates a generic return type `<T extends Base>` for the `get()` method. However, in `C1Repo`, you're specifying that the method returns a specific class, `C1`.

    This creates a mismatch between what the interface promises (`T extends Base`) and what the implementing class provides (`C1`). While it compiles, it bypasses compile-time type checks, potentially leading to runtime errors if the returned type doesn't match expectations.

    To address this and ensure type safety, you can modify the `Repo` interface to directly specify the return type:

    ```java
    public interface Repo {
    Base get();
    }
    ```

    With this change, any class implementing `Repo` must return an instance of `Base` or its subtypes. Your `C1Repo` implementation would then look like this:

    ```java
    public class C1Repo implements Repo {
    @Override
    public C1 get() {
    return new C1();
    }
    }
    ```

    Certainly, here's the revised last paragraph:

    This modification ensures that the return type matches the interface's contract, eliminating the warning and ensuring type safety at compile time. Now, any implementation of `Repo` must adhere to returning a `Base` or its subclass, which aligns with the interface's intention. If you need further help with Java assignment, feel free to explore resources available online like ProgrammingHomeworkHelp.com.

Similar Threads

  1. What's a good resource to practice Algorithms?
    By sjain100 in forum Algorithms & Recursion
    Replies: 3
    Last Post: August 14th, 2024, 07:11 AM
  2. Question about good coding practice
    By Cornix in forum Java Theory & Questions
    Replies: 7
    Last Post: August 29th, 2014, 08:32 AM
  3. Any good websites to practice programming?
    By SauronWatchesYou in forum The Cafe
    Replies: 1
    Last Post: April 17th, 2014, 08:06 AM
  4. 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
  5. abstract class & 'covariant' return type?
    By jezza10181 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2011, 02:26 PM

Tags for this Thread