Java throwing error “ is not abstract and does not override abstract method in the

前端 未结 3 803
小鲜肉
小鲜肉 2020-12-12 03:10

I am unable to compile the below code written, Java always throws an error. Can you please help me out (P.S: I am new to Java, in a learning stage still). If anywhere, i hav

相关标签:
3条回答
  • 2020-12-12 03:19

    When you implement an interface you need to implement all the methods of that interface in the implementing class.

    In your example you should implement add(), sub(), mul() and div() in any class that implements the interface calci

    0 讨论(0)
  • 2020-12-12 03:28

    You probably want to read up a little on interfaces. They are one of the strongest aspects of Java and object-oriented design in general.

    If you create an interface called "calci," you are creating a contract that any class implementing the interface will implement what you defined in calci. So you may want to rethink how this is designed currently.

    To describe how it works, change the interface to only have one method called "perform." Now, in the implementing classes addition, subtraction, multiplication, and division, create a method also called "perform." You can call it almost anything other than "perform," but make it a characteristic that you feel all classes that calci implements or other interfaces that extend calci should guarantee.

    Then it will be possible to do:

    calci addition = new addition();
    addition.perform();
    
    calci multiplication = new multiplication();
    multiplication.perform();
    

    etc.

    You can use the @Override annotation on the implementing method to make sure that misspellings are caught in your IDE (like Eclipse).

    0 讨论(0)
  • 2020-12-12 03:38

    Well as the error says - certain classes that implement the calci interface do not implement all the methods of that interface. When you are creating a class that implements an interface you need to implement all the methods from that interface, not only certain ones you are interested in. That's the whole point of interfaces. Otherwise you wouldn't be able to code against interfaces as for instance calci test = getCalciInstance().XXX() where XXX() is a method declared in calci might not be implemented depending on which implementation's instance getCalciInstance() would return!

    So all your classes need to implement the following methods:

    void add();
    void sub();
    void mul();
    void div();
    
    0 讨论(0)
提交回复
热议问题