Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingOther conceptsNested classes

Anonymous classes properties

Calculator

Report a typo

There is an abstract class Calculator:

abstract class Calculator {
    
    public abstract long sum(long val1, long val2);
    
    public abstract long subtraction(long val1, long val2);
}

You should create an anonymous class that extends the given class and assign the instance to the variable anonymousCalculator.

The anonymous class must override both abstract methods of the class. The method sum should return the sum of its arguments. The method subtraction should return the difference between the first and second arguments.

Write a program in Java 17
class CalculatorWrapper {
static Calculator anonymousCalculator = /* create an instance of an anonymous class here,
do not forget ; in the end */
}

abstract class Calculator {

public abstract long sum(long val1, long val2);

public abstract long subtraction(long val1, long val2);
}
___

Create a free account to access the full topic