Implementing an Interface

Implementing an Interface

When we would like to implement an interface , we\'re agreeing to adhere to the contract defined
in the interface. its mean we are authorized person for the provide legal implementations for
every method defined in the interface,and that anyone who knows what the interface methods look like (not how they\'re implemented, but how they can be called and what they return) can rest assured that they can invoke those methods on an instance of our implementing class.

We looked earlier at several examples of implementation classes, including the Ball class that implements Bounceable, but the following class would also compile legally:

public class Ball implements Bounceable { // Keyword \'implements\'
public void bounce() { }
public void setBounceFactor(int bf) { }

Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following:

  1. Provide concrete (nonabstract) implementations for all methods from the declared interface.
  2. Follow all the rules for legal overrides
  3. Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.
  4. Maintain the signature of the interface method, and maintain the same return type (but does not have to declare the exceptions declared in the interface method declaration).

Date:2025-02-09 00:00:00

Post Your Answers

User Email:

User Name:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher