How to @Override an Attribute declared in an Interface implemented in a class












-2















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.










share|improve this question























  • 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
















-2















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.










share|improve this question























  • 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














-2












-2








-2








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 inherently static and final.

    – 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













  • 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












2 Answers
2






active

oldest

votes


















2














String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.






share|improve this answer
























  • 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



















0














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






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%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









    2














    String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.






    share|improve this answer
























    • 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
















    2














    String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.






    share|improve this answer
























    • 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














    2












    2








    2







    String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.






    share|improve this answer













    String Bezeichnungi inherited from the interface into the class is final and hence cannot be overridden.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 11:48









        ItFreakItFreak

        774115




        774115






























            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%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





















































            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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma