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 6722431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:26:47+00:00 2026-05-26T09:26:47+00:00

I have an interface Node that asks for the method: public HashSet getNeighbour(); NodeVariable

  • 0

I have an interface Node that asks for the method:

public HashSet getNeighbour();

NodeVariable implements Node, and its neighbours are of type NodeFunction (that implements Node, too), and I wrote the method:

public HashSet<NodeFunction> getNeighbour();

(and viceversa in NodeFunction class).

I found out that if I change the signature of method in Node to:

public HashSet<Node> getNeighbour();

then on the methods in NodeVariable and NodeFunction I get the error:

Error getNeighbour() in factorgraph.NodeFunction cannot implement getNeighbour() in factorgraph.Node return type java.util.HashSet is not compatible with java.util.HashSet NodeFunction.java

This is not really clear.

I found:

Overriding return type in extended interface – Bad idea?

and

Java – Overriding return type of extended interface when return type uses generics for own method parameter types

and now I changed the Node method signature in:

public HashSet<? extends Node> getNeighbour();

thus the compiler stops complaining.

Is it right? Why HashSet is not considered like an “extension” of HashSet?

  • 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-05-26T09:26:48+00:00Added an answer on May 26, 2026 at 9:26 am

    HashSet<Node> and HashSet<NodeFunction> aren’t compatible, even though NodeFunction implements/subclasses Node. Similarly, neither are List<Number> and List<Integer>. Integer subclasses Number.

    static List<Number> getNumberList(int size) {
        //ArrayList<Integer> numList = null; //Doesn't compile
        ArrayList<Number> numList = null; //Compiles
        return numList;
    }
    

    If the compiler allowed what your trying to do, then I could do the following and a ClassCastException would be thrown, which is the exact reason generics was created.

    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
        public static void main( String[] args ) {
            Node nd = getInstance();
            Set<Node> ndSet = nd.getNeighbour();
            ndSet.add( new NodeSign() );
            nd.removeSingleNeighbor(); //throws ClassCastException
        }
    
        static Node getInstance() {
            return new NodeVariable();
        }
    }
    
    interface Node {
        public Set<Node> getNeighbour();
        public void removeSingleNeighbor();
    }
    
    class NodeVariable implements Node {
        Set<NodeFunction> ndFuncList = new HashSet<NodeFunction>();
        public Set<NodeFunction> getNeighbour(){ return ndFuncList; } //wont' compile
    
        //HERE!!!!
    
        public void removeSingleNeighbor() { 
            NodeFunction ndFunc = (NodeFunction)ndFuncList.toArray()[ndFuncList.size()-1]; //throws ClassCastException
        }
    }
    
    class NodeFunction implements Node {
        public Set<NodeFunction> getNeighbour(){ return null; } //won't compile
        public void removeSingleNeighbor() {}
    }
    
    class NodeSign implements Node {
        public Set<NodeFunction> getNeighbour(){ return null; } //won't compile
        public void removeSingleNeighbor() {}
    }
    

    Everything is semantically/syntactically valid except public Set<NodeFunction> getNeighbour(){}. The Java tutorials cover this issue.

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

Sidebar

Related Questions

So I have 2 interfaces: A node that can have children public interface INode
So I have made this simple interface: package{ public interface GraphADT{ function addNode(newNode:Node):Boolean; }
I've written a class that implements IEnumerable<T> . I have a method that returns
If I have interface IFoo, and have several classes that implement it, what is
Lets say you have interface definition. That interface can be Operation . Then you
Have a interface class abc { public: virtual int foo() = 0; ... }
I have an Interface called IStep that can do some computation (See Execution in
I have an interface that I have defined in C++ which now needs to
I have an interface - here's a nicely contrived version as an example: public
I have a interface that defines some methods with attributes. These attributes need to

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.