Adarsh Kumar
Adarsh Kumar

Reputation: 11

Java throwing error "<Class Name> is not abstract and does not override abstract method in the <Interface>"

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 have written the code wrong, please help me in correcting it so that i can learn it well.

Error Thrown (4) : 1. Addition is not abstract and does not override abstract method Div() in calci

  1. Subtraction is not abstract and does not override abstract method Div() in calci

  2. Division is not abstract and does not override abstract method Div() in calci

  3. Multiplication is not abstract and does not override abstract method Div() in calci

    import java.util.Scanner;
    
    interface calci    
    
    {
    
    
        void add();
        void sub();
        void mul();
        void div();
    }
    

Class Addition

class addition implements calci   

{

public void add()    

    {
        System.out.println("Enter two numbers:");
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int n1= scn.nextInt();
        int result = n+n1;
        System.out.println("The Result of the two numbers is:"+result);
    }
}

class subtraction

class subtraction implements calci

{

    public void sub()`enter code here`
    {
        System.out.println("Enter two numbers:");
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int n1= scn.nextInt();
        int result = n-n1;
        System.out.println("The Result of the two numbers is:"+result);
    }
}

class multiplication

class multiplication implements calci

{

    public void mul()
    {
        System.out.println("Enter two numbers:");
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int n1= scn.nextInt();
        int result = n*n1;
        System.out.println("The Result of the two numbers is:"+result);
    }
}

class division

class division implements calci

{

    public void div()
    {
        System.out.println("Enter two numbers:");
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int n1= scn.nextInt();
        int result = n/n1;
        System.out.println("The Result of the two numbers is:"+result);
    }
}

class calculator1

class calculator1

{

    public static void main(String[] args)
    {
        int i =0;
        addition a1 = new addition();
        subtraction s1 = new subtraction();
        multiplication m1 = new multiplication();
        division d1 = new division();
        System.out.println("Enter Your Name:");
        Scanner scn = new Scanner(System.in);
        String a = scn.next();
        System.out.println("Good Morning!"+a);
        System.out.println();
        System.out.println("Please choose option from below");
        System.out.println();
        System.out.println("1.Addition 2.Subtraction 3.Multiplication 4.Division");
        int option = scn.nextInt();
        while (i!=0)
        {
        if (option==1)
        {
            a1.add();
        }
        else if (option == 2)
        {
            s1.sub();
        }
        else if (option == 3)
        {
            m1.mul();
        }
        else if (option == 4)
        {
            d1.div();
        }
        else
        {
            System.out.println("Please enter valid number");
        }
        }
        System.out.println("Do you wish to continue");
        int b = scn.nextInt();
        if (b==0)
        {
            System.out.println("Thanks for using the calculator Program"+a);
            System.out.println("Have a great day!");
        }
    }


}

Upvotes: 1

Views: 1909

Answers (3)

riddle_me_this
riddle_me_this

Reputation: 9125

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).

Upvotes: 0

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

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();

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53525

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

Upvotes: 1

Related Questions