Java and property overload










0















I'm stucked on this issue using eclipse 2018-12 (4.10) / Java 11 on debian 9



Let's state I've a base class



class A 

private typeA prop;

TypeA getProp()
return this.prop;




and a derived one



class B extends A{
private typeB prop;

TypeB getProp()
return this.prop;



I'm expecting that prop in class B hides the prop in class A, but in eclipse I've got an error Message in class B for the getProp method :
The return type is incompatible with A.getProp()



any idea ?










share|improve this question
























  • You overrided getProp; name them differently.

    – rgettman
    Nov 14 '18 at 18:07











  • Any method is not overridden if it just differs by return type.

    – CS_noob
    Nov 14 '18 at 18:08











  • Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

    – Mark
    Nov 14 '18 at 18:08












  • I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

    – Emmanuel
    Nov 14 '18 at 18:08






  • 3





    That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

    – JB Nizet
    Nov 14 '18 at 18:09
















0















I'm stucked on this issue using eclipse 2018-12 (4.10) / Java 11 on debian 9



Let's state I've a base class



class A 

private typeA prop;

TypeA getProp()
return this.prop;




and a derived one



class B extends A{
private typeB prop;

TypeB getProp()
return this.prop;



I'm expecting that prop in class B hides the prop in class A, but in eclipse I've got an error Message in class B for the getProp method :
The return type is incompatible with A.getProp()



any idea ?










share|improve this question
























  • You overrided getProp; name them differently.

    – rgettman
    Nov 14 '18 at 18:07











  • Any method is not overridden if it just differs by return type.

    – CS_noob
    Nov 14 '18 at 18:08











  • Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

    – Mark
    Nov 14 '18 at 18:08












  • I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

    – Emmanuel
    Nov 14 '18 at 18:08






  • 3





    That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

    – JB Nizet
    Nov 14 '18 at 18:09














0












0








0








I'm stucked on this issue using eclipse 2018-12 (4.10) / Java 11 on debian 9



Let's state I've a base class



class A 

private typeA prop;

TypeA getProp()
return this.prop;




and a derived one



class B extends A{
private typeB prop;

TypeB getProp()
return this.prop;



I'm expecting that prop in class B hides the prop in class A, but in eclipse I've got an error Message in class B for the getProp method :
The return type is incompatible with A.getProp()



any idea ?










share|improve this question
















I'm stucked on this issue using eclipse 2018-12 (4.10) / Java 11 on debian 9



Let's state I've a base class



class A 

private typeA prop;

TypeA getProp()
return this.prop;




and a derived one



class B extends A{
private typeB prop;

TypeB getProp()
return this.prop;



I'm expecting that prop in class B hides the prop in class A, but in eclipse I've got an error Message in class B for the getProp method :
The return type is incompatible with A.getProp()



any idea ?







java inheritance properties field






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:09







Emmanuel

















asked Nov 14 '18 at 18:05









EmmanuelEmmanuel

4771931




4771931












  • You overrided getProp; name them differently.

    – rgettman
    Nov 14 '18 at 18:07











  • Any method is not overridden if it just differs by return type.

    – CS_noob
    Nov 14 '18 at 18:08











  • Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

    – Mark
    Nov 14 '18 at 18:08












  • I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

    – Emmanuel
    Nov 14 '18 at 18:08






  • 3





    That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

    – JB Nizet
    Nov 14 '18 at 18:09


















  • You overrided getProp; name them differently.

    – rgettman
    Nov 14 '18 at 18:07











  • Any method is not overridden if it just differs by return type.

    – CS_noob
    Nov 14 '18 at 18:08











  • Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

    – Mark
    Nov 14 '18 at 18:08












  • I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

    – Emmanuel
    Nov 14 '18 at 18:08






  • 3





    That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

    – JB Nizet
    Nov 14 '18 at 18:09

















You overrided getProp; name them differently.

– rgettman
Nov 14 '18 at 18:07





You overrided getProp; name them differently.

– rgettman
Nov 14 '18 at 18:07













Any method is not overridden if it just differs by return type.

– CS_noob
Nov 14 '18 at 18:08





Any method is not overridden if it just differs by return type.

– CS_noob
Nov 14 '18 at 18:08













Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

– Mark
Nov 14 '18 at 18:08






Besides your issue your code does not compile, you have a method that returns TypeB and you're trying to return a variable of type typeB. Also you're missing semicolons.

– Mark
Nov 14 '18 at 18:08














I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

– Emmanuel
Nov 14 '18 at 18:08





I just don't want to name the differently .. why getProp() in class B does not overload getProp in class A ?

– Emmanuel
Nov 14 '18 at 18:08




3




3





That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

– JB Nizet
Nov 14 '18 at 18:09






That would completely break polymorphism, and the Liskov principle. An A has a getProp() that returns a TypeA. B extends A. So any instance of B is an instance of A. So calling getProp() on a B instance must return a TypeA: that's the contract of the class A. Just take this code, that is supposed to work: A a = new B(); TypeA typeA = a.getProp();. Your code would make that impossible.

– JB Nizet
Nov 14 '18 at 18:09













2 Answers
2






active

oldest

votes


















0














Solved using JB Nizet comment through TypeA inheritance -> TypeB






share|improve this answer






























    0














    you can try using interfaces like this:



    interface Type 
    // ...


    class TypeA implements Type
    // ...


    class TypeB implements Type
    // ...


    class A

    private TypeA prop;

    Type getProp()
    return this.prop;



    class B extends A

    private TypeB prop;

    Type getProp()
    return this.prop;







    share|improve this answer






















      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%2f53306332%2fjava-and-property-overload%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









      0














      Solved using JB Nizet comment through TypeA inheritance -> TypeB






      share|improve this answer



























        0














        Solved using JB Nizet comment through TypeA inheritance -> TypeB






        share|improve this answer

























          0












          0








          0







          Solved using JB Nizet comment through TypeA inheritance -> TypeB






          share|improve this answer













          Solved using JB Nizet comment through TypeA inheritance -> TypeB







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 18:19









          EmmanuelEmmanuel

          4771931




          4771931























              0














              you can try using interfaces like this:



              interface Type 
              // ...


              class TypeA implements Type
              // ...


              class TypeB implements Type
              // ...


              class A

              private TypeA prop;

              Type getProp()
              return this.prop;



              class B extends A

              private TypeB prop;

              Type getProp()
              return this.prop;







              share|improve this answer



























                0














                you can try using interfaces like this:



                interface Type 
                // ...


                class TypeA implements Type
                // ...


                class TypeB implements Type
                // ...


                class A

                private TypeA prop;

                Type getProp()
                return this.prop;



                class B extends A

                private TypeB prop;

                Type getProp()
                return this.prop;







                share|improve this answer

























                  0












                  0








                  0







                  you can try using interfaces like this:



                  interface Type 
                  // ...


                  class TypeA implements Type
                  // ...


                  class TypeB implements Type
                  // ...


                  class A

                  private TypeA prop;

                  Type getProp()
                  return this.prop;



                  class B extends A

                  private TypeB prop;

                  Type getProp()
                  return this.prop;







                  share|improve this answer













                  you can try using interfaces like this:



                  interface Type 
                  // ...


                  class TypeA implements Type
                  // ...


                  class TypeB implements Type
                  // ...


                  class A

                  private TypeA prop;

                  Type getProp()
                  return this.prop;



                  class B extends A

                  private TypeB prop;

                  Type getProp()
                  return this.prop;








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 18:22









                  elbraulioelbraulio

                  746213




                  746213



























                      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%2f53306332%2fjava-and-property-overload%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線