incompatible type wile returning generic method of statictype





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I tried to implement a BST class was like



class BST<T> {
private Node<T> root;
private static class Node<T> {
T element;
Node<T> left;
Node<T> right;
}
}


I want to get the successor node in a method which takes a Node as a parameter and returns a successor node



private <T> Node<T> get(Node<T> current node){  
Node<T> successor = root;
}


here I'm getting incompatible types . if I remove the method bound



private Node<T> get(Node<T> current node) {
Node<T> successor = root;
}


now it is compiling fine. What is the reason?










share|improve this question































    3















    I tried to implement a BST class was like



    class BST<T> {
    private Node<T> root;
    private static class Node<T> {
    T element;
    Node<T> left;
    Node<T> right;
    }
    }


    I want to get the successor node in a method which takes a Node as a parameter and returns a successor node



    private <T> Node<T> get(Node<T> current node){  
    Node<T> successor = root;
    }


    here I'm getting incompatible types . if I remove the method bound



    private Node<T> get(Node<T> current node) {
    Node<T> successor = root;
    }


    now it is compiling fine. What is the reason?










    share|improve this question



























      3












      3








      3








      I tried to implement a BST class was like



      class BST<T> {
      private Node<T> root;
      private static class Node<T> {
      T element;
      Node<T> left;
      Node<T> right;
      }
      }


      I want to get the successor node in a method which takes a Node as a parameter and returns a successor node



      private <T> Node<T> get(Node<T> current node){  
      Node<T> successor = root;
      }


      here I'm getting incompatible types . if I remove the method bound



      private Node<T> get(Node<T> current node) {
      Node<T> successor = root;
      }


      now it is compiling fine. What is the reason?










      share|improve this question
















      I tried to implement a BST class was like



      class BST<T> {
      private Node<T> root;
      private static class Node<T> {
      T element;
      Node<T> left;
      Node<T> right;
      }
      }


      I want to get the successor node in a method which takes a Node as a parameter and returns a successor node



      private <T> Node<T> get(Node<T> current node){  
      Node<T> successor = root;
      }


      here I'm getting incompatible types . if I remove the method bound



      private Node<T> get(Node<T> current node) {
      Node<T> successor = root;
      }


      now it is compiling fine. What is the reason?







      java generics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 4:01









      Naman

      45.9k11102204




      45.9k11102204










      asked Jan 4 at 2:58









      user3878073user3878073

      3015




      3015
























          2 Answers
          2






          active

          oldest

          votes


















          1














          reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();






          share|improve this answer































            0














            Your current code(which doesn't compile) reads as :



            static class BST<T> {
            private Node<T> root;

            private static class Node<T> {
            T element;
            Node<T> left;
            Node<T> right;
            }

            //
            private <S> Node<T> get(Node<T> currentNode) {
            Node<S> successor = root;
            return null;
            }
            }


            now you could easily relate by Node<S> couldn't be compatible with Node<T>, right?



            Because of the presence of the method bound, the compiler treats both the method bound and the successor bound to be the same in this case. But the bound represented in the class BST<T> is not the same as bound represented by <T> Node<T> get method further still and that's where the incompatibility arises from.



            If you wish to use the same bound, you could simply change the above code to get rid of your method bound, which is where you don't see any compile errors. Since then the bound of successor is inferred as the same as the bound of the class BST.






            share|improve this answer



















            • 1





              The scope of declaration could be worth reading for the specifications related to this.

              – Naman
              Jan 4 at 4:02














            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032590%2fincompatible-type-wile-returning-generic-method-of-statictype%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();






            share|improve this answer




























              1














              reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();






              share|improve this answer


























                1












                1








                1







                reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();






                share|improve this answer













                reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 3:02









                mkjhmkjh

                1,1751023




                1,1751023

























                    0














                    Your current code(which doesn't compile) reads as :



                    static class BST<T> {
                    private Node<T> root;

                    private static class Node<T> {
                    T element;
                    Node<T> left;
                    Node<T> right;
                    }

                    //
                    private <S> Node<T> get(Node<T> currentNode) {
                    Node<S> successor = root;
                    return null;
                    }
                    }


                    now you could easily relate by Node<S> couldn't be compatible with Node<T>, right?



                    Because of the presence of the method bound, the compiler treats both the method bound and the successor bound to be the same in this case. But the bound represented in the class BST<T> is not the same as bound represented by <T> Node<T> get method further still and that's where the incompatibility arises from.



                    If you wish to use the same bound, you could simply change the above code to get rid of your method bound, which is where you don't see any compile errors. Since then the bound of successor is inferred as the same as the bound of the class BST.






                    share|improve this answer



















                    • 1





                      The scope of declaration could be worth reading for the specifications related to this.

                      – Naman
                      Jan 4 at 4:02


















                    0














                    Your current code(which doesn't compile) reads as :



                    static class BST<T> {
                    private Node<T> root;

                    private static class Node<T> {
                    T element;
                    Node<T> left;
                    Node<T> right;
                    }

                    //
                    private <S> Node<T> get(Node<T> currentNode) {
                    Node<S> successor = root;
                    return null;
                    }
                    }


                    now you could easily relate by Node<S> couldn't be compatible with Node<T>, right?



                    Because of the presence of the method bound, the compiler treats both the method bound and the successor bound to be the same in this case. But the bound represented in the class BST<T> is not the same as bound represented by <T> Node<T> get method further still and that's where the incompatibility arises from.



                    If you wish to use the same bound, you could simply change the above code to get rid of your method bound, which is where you don't see any compile errors. Since then the bound of successor is inferred as the same as the bound of the class BST.






                    share|improve this answer



















                    • 1





                      The scope of declaration could be worth reading for the specifications related to this.

                      – Naman
                      Jan 4 at 4:02
















                    0












                    0








                    0







                    Your current code(which doesn't compile) reads as :



                    static class BST<T> {
                    private Node<T> root;

                    private static class Node<T> {
                    T element;
                    Node<T> left;
                    Node<T> right;
                    }

                    //
                    private <S> Node<T> get(Node<T> currentNode) {
                    Node<S> successor = root;
                    return null;
                    }
                    }


                    now you could easily relate by Node<S> couldn't be compatible with Node<T>, right?



                    Because of the presence of the method bound, the compiler treats both the method bound and the successor bound to be the same in this case. But the bound represented in the class BST<T> is not the same as bound represented by <T> Node<T> get method further still and that's where the incompatibility arises from.



                    If you wish to use the same bound, you could simply change the above code to get rid of your method bound, which is where you don't see any compile errors. Since then the bound of successor is inferred as the same as the bound of the class BST.






                    share|improve this answer













                    Your current code(which doesn't compile) reads as :



                    static class BST<T> {
                    private Node<T> root;

                    private static class Node<T> {
                    T element;
                    Node<T> left;
                    Node<T> right;
                    }

                    //
                    private <S> Node<T> get(Node<T> currentNode) {
                    Node<S> successor = root;
                    return null;
                    }
                    }


                    now you could easily relate by Node<S> couldn't be compatible with Node<T>, right?



                    Because of the presence of the method bound, the compiler treats both the method bound and the successor bound to be the same in this case. But the bound represented in the class BST<T> is not the same as bound represented by <T> Node<T> get method further still and that's where the incompatibility arises from.



                    If you wish to use the same bound, you could simply change the above code to get rid of your method bound, which is where you don't see any compile errors. Since then the bound of successor is inferred as the same as the bound of the class BST.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 4 at 3:55









                    NamanNaman

                    45.9k11102204




                    45.9k11102204








                    • 1





                      The scope of declaration could be worth reading for the specifications related to this.

                      – Naman
                      Jan 4 at 4:02
















                    • 1





                      The scope of declaration could be worth reading for the specifications related to this.

                      – Naman
                      Jan 4 at 4:02










                    1




                    1





                    The scope of declaration could be worth reading for the specifications related to this.

                    – Naman
                    Jan 4 at 4:02







                    The scope of declaration could be worth reading for the specifications related to this.

                    – Naman
                    Jan 4 at 4:02




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032590%2fincompatible-type-wile-returning-generic-method-of-statictype%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas