Sobes.tech
Junior — Middle

What steps should be taken if two methods are declared in the interface or abstract class?

sobes.tech AI

Answer from AI

If two methods are declared in an interface or an abstract class, then when implementing this interface or inheriting from an abstract class, it is necessary to:

  1. Always implement both methods if they do not have default implementations (in interfaces with Java 8+ default methods are possible).
  2. If the methods have the same signatures (for example, in the case of multiple inheritance of interfaces with identical methods), then you need to explicitly override the method in the class to resolve the conflict.

Example for an interface:

interface A {
    void method1();
    void method2();
}

class B implements A {
    @Override
    public void method1() {
        System.out.println("Implementation of method1");
    }

    @Override
    public void method2() {
        System.out.println("Implementation of method2");
    }
}

If an abstract class contains two abstract methods, then the subclass is obliged to implement them; otherwise, it must be declared abstract itself.