Java and property overload
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
|
show 5 more comments
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
You overridedgetProp
; 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 returnsTypeB
and you're trying to return a variable of typetypeB
. 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
|
show 5 more comments
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
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
java inheritance properties field
edited Nov 14 '18 at 18:09
Emmanuel
asked Nov 14 '18 at 18:05
EmmanuelEmmanuel
4771931
4771931
You overridedgetProp
; 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 returnsTypeB
and you're trying to return a variable of typetypeB
. 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
|
show 5 more comments
You overridedgetProp
; 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 returnsTypeB
and you're trying to return a variable of typetypeB
. 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
|
show 5 more comments
2 Answers
2
active
oldest
votes
Solved using JB Nizet comment through TypeA inheritance -> TypeB
add a comment |
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Solved using JB Nizet comment through TypeA inheritance -> TypeB
add a comment |
Solved using JB Nizet comment through TypeA inheritance -> TypeB
add a comment |
Solved using JB Nizet comment through TypeA inheritance -> TypeB
Solved using JB Nizet comment through TypeA inheritance -> TypeB
answered Nov 14 '18 at 18:19
EmmanuelEmmanuel
4771931
4771931
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 14 '18 at 18:22
elbraulioelbraulio
746213
746213
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 typetypeB
. 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