Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:50:07+00:00 2026-06-01T19:50:07+00:00

If I have a method, for instance public INode getNode(final int offset); I assume

  • 0

If I have a method, for instance

public INode getNode(final int offset);

I assume it doesn’t add something to make the method return type generic, for instance:

public <T extends INode> T getNode(final int offset);

Or did I miss something? I think generic return types are only of value if one of the parameters is of the same type (or a super/subtype)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:50:09+00:00Added an answer on June 1, 2026 at 7:50 pm
    public <T extends INode> T getNode(final int offset);
    

    Not only does this not provide any additional information to the caller, but is outright dangerous: The only way to implement that method signature is to use an unchecked cast, which can not be type safe, because a method’s type parameters are specified by its caller (explicitly or implictly through type inference), and the type parameters aren’t available to this method’s implementation. For instance, consider the following program:

    class NodeCollection {
        private INode[] nodes = new INode[42];
    
        public <T extends INode> T getNode(final int offset) {
            return (T) nodes[offset];
        }
    
        public <T extends INode> setNode(final int offset, T node) {
            nodes[offset] = node;
        }
    }
    
    class ANode implements INode {}
    class BNode implements INode {
        void foo();
    }
    
    public class Test {
        public static void main(String[] args) {
            NodeCollection nc = new NodeCollection();
            nc.setNode(0,new ANode());
            BNode b = nc.getNode(0); // throws ClassCastException (sic!)
        }
    }
    

    Best practice: Don’t use an unchecked cast, unless you are really sure it’ll be type correct at runtime.

    I think generic return types are only of value if one of the parameters is of the same type (or a super/subtype)?

    There are more cases, for instance:

    public <T> T getFavorite(Class<T> clazz) {
        return clazz.cast(favorites.get(clazz));
    }
    

    or

    interface List<E> {
        E get(int index);
    }
    

    or the examples in Colin’s answer, where the type variable merely appears as type parameter in the return type, which is acceptable due to type erasure.

    Edit:

    I think there’s no type save way if one wants to cast to the exact type of node (instead of instanceof has to precede it)

    Of course there is, it’s called visitor pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method called public int getSize() and it is suppose to return
I have a method where I would like to return an object instance of
I have a public class which has the following method and instance variable: public
I have a method in Java: public int getInt() { IntByReference ibr = new
I want to have a method like this on my DocumentFilter public void replaceUpdate(int
I have a method like this public static DataSet GetAllDataBaseNames() { //Instance of connection
I have a class: class A { public: static A& instance(); ... void setValue(int
I have a method where I READ objects from DB, for instance: public Object
If I have a class Sample and I have an instance method, instanceMethod in
For instance I have a method SomeMethod(Graphics g) { ... } If I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.