How to @Override an Attribute declared in an Interface implemented in a class
Interface:
package II_1_b;
public interface Bezeichnung {
public String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
class:
package II_1_b;
public class Speerwurf extends SportDaten implements Bezeichnung {
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
You can see our Code here, I commented the problem area and hope you can help us. Stackoverflow tells me to add more details, so I'm gonna describe what I'm going to have for lunch: I think I will make myself a TK-Pizza, maybe 2. I'm often very hungry.
java interface override implementation
add a comment |
Interface:
package II_1_b;
public interface Bezeichnung {
public String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
class:
package II_1_b;
public class Speerwurf extends SportDaten implements Bezeichnung {
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
You can see our Code here, I commented the problem area and hope you can help us. Stackoverflow tells me to add more details, so I'm gonna describe what I'm going to have for lunch: I think I will make myself a TK-Pizza, maybe 2. I'm often very hungry.
java interface override implementation
If I understand what you're asking, you can't. Fields declared in interfaces are inherentlystatic
andfinal
.
– Slaw
Nov 14 '18 at 11:27
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50
add a comment |
Interface:
package II_1_b;
public interface Bezeichnung {
public String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
class:
package II_1_b;
public class Speerwurf extends SportDaten implements Bezeichnung {
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
You can see our Code here, I commented the problem area and hope you can help us. Stackoverflow tells me to add more details, so I'm gonna describe what I'm going to have for lunch: I think I will make myself a TK-Pizza, maybe 2. I'm often very hungry.
java interface override implementation
Interface:
package II_1_b;
public interface Bezeichnung {
public String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
class:
package II_1_b;
public class Speerwurf extends SportDaten implements Bezeichnung {
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
You can see our Code here, I commented the problem area and hope you can help us. Stackoverflow tells me to add more details, so I'm gonna describe what I'm going to have for lunch: I think I will make myself a TK-Pizza, maybe 2. I'm often very hungry.
java interface override implementation
java interface override implementation
asked Nov 14 '18 at 11:25
Master of the UniverseMaster of the Universe
1
1
If I understand what you're asking, you can't. Fields declared in interfaces are inherentlystatic
andfinal
.
– Slaw
Nov 14 '18 at 11:27
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50
add a comment |
If I understand what you're asking, you can't. Fields declared in interfaces are inherentlystatic
andfinal
.
– Slaw
Nov 14 '18 at 11:27
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50
If I understand what you're asking, you can't. Fields declared in interfaces are inherently
static
and final
.– Slaw
Nov 14 '18 at 11:27
If I understand what you're asking, you can't. Fields declared in interfaces are inherently
static
and final
.– Slaw
Nov 14 '18 at 11:27
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50
add a comment |
2 Answers
2
active
oldest
votes
String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
add a comment |
As @slaw stated, fields in interfaces cannot be changed and are thus static and final.
Additionally, there is no sense of declaring fields in an interface, because it only declares a certain behaviour and not a state. To make things work like you showed here, you need to use an abstract
class:
package II_1_b;
public abstract class Bezeichnung {
public protected String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
Concrete class:
package II_1_b;
public class Speerwurf extends Bezeichnung { //think about how to handle SportDaten!
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
Since we dont know your concrete use case, we cannot help you except of telling you why it does not work the way it should
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%2f53299113%2fhow-to-override-an-attribute-declared-in-an-interface-implemented-in-a-class%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
String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
add a comment |
String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
add a comment |
String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.
String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.
answered Nov 14 '18 at 11:33
abj1305abj1305
18310
18310
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
add a comment |
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
you don't "inherit" from interfaces. it's accessed through the interface.
– Stultuske
Nov 14 '18 at 12:06
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
My apologies if i conveyed my point with an incorrect statement. Anyways, thanks for correcting.
– abj1305
Nov 14 '18 at 12:15
add a comment |
As @slaw stated, fields in interfaces cannot be changed and are thus static and final.
Additionally, there is no sense of declaring fields in an interface, because it only declares a certain behaviour and not a state. To make things work like you showed here, you need to use an abstract
class:
package II_1_b;
public abstract class Bezeichnung {
public protected String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
Concrete class:
package II_1_b;
public class Speerwurf extends Bezeichnung { //think about how to handle SportDaten!
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
Since we dont know your concrete use case, we cannot help you except of telling you why it does not work the way it should
add a comment |
As @slaw stated, fields in interfaces cannot be changed and are thus static and final.
Additionally, there is no sense of declaring fields in an interface, because it only declares a certain behaviour and not a state. To make things work like you showed here, you need to use an abstract
class:
package II_1_b;
public abstract class Bezeichnung {
public protected String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
Concrete class:
package II_1_b;
public class Speerwurf extends Bezeichnung { //think about how to handle SportDaten!
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
Since we dont know your concrete use case, we cannot help you except of telling you why it does not work the way it should
add a comment |
As @slaw stated, fields in interfaces cannot be changed and are thus static and final.
Additionally, there is no sense of declaring fields in an interface, because it only declares a certain behaviour and not a state. To make things work like you showed here, you need to use an abstract
class:
package II_1_b;
public abstract class Bezeichnung {
public protected String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
Concrete class:
package II_1_b;
public class Speerwurf extends Bezeichnung { //think about how to handle SportDaten!
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
Since we dont know your concrete use case, we cannot help you except of telling you why it does not work the way it should
As @slaw stated, fields in interfaces cannot be changed and are thus static and final.
Additionally, there is no sense of declaring fields in an interface, because it only declares a certain behaviour and not a state. To make things work like you showed here, you need to use an abstract
class:
package II_1_b;
public abstract class Bezeichnung {
public protected String Bezeichnungi = "Hallo";
public abstract void setBezeichnung();
}
Concrete class:
package II_1_b;
public class Speerwurf extends Bezeichnung { //think about how to handle SportDaten!
private double weite;
@Override
public void setBezeichnung(){ //Here we want to Override the String in
Bezeichnungi = "Test"; //the Interface
}
public Speerwurf(String n, double w, String bez) {
super(n);
this.weite = w;
bez = Bezeichnungi;
}
@Override
public void display() {
System.out.println("Speerwurf von " + this.SportlerName + ":n"
+ weite + " Meter " + Bezeichnungi);
}
}
Since we dont know your concrete use case, we cannot help you except of telling you why it does not work the way it should
answered Nov 14 '18 at 11:48
ItFreakItFreak
774115
774115
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%2f53299113%2fhow-to-override-an-attribute-declared-in-an-interface-implemented-in-a-class%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
If I understand what you're asking, you can't. Fields declared in interfaces are inherently
static
andfinal
.– Slaw
Nov 14 '18 at 11:27
could you show your class SportDaten?
– ItFreak
Nov 14 '18 at 11:50