avoid implementation of a method which is there in interface - java

21,700

Solution 1

public interface a{

      public void m1();
      public void m2();
      public void m3();

}

public abstract class A implements a{

       public void m3(){

           // implementation code     

           }

}

Declare as abstract class so you will not needed to implement these methods in this class. But you have to implement those method in concrete class

Solution 2

TLDR;

If the interface is given to you, then you have no choice. The creator of the interface forces you to implement all of its methods.

If you are writing the interface, then you are probably mistaking. If you want to implement only a subset of the methods, then you would probably be better off with an abstract class.

In details

An interface declares a behavioral contract. Its purpose is precisely to force all implementing classes to implement all of its methods, thus ensuring the implementing classes are compliant with the contract.

For example, the following interface:

public interface highlightable {

    public void highlight();

}

declares that every implementing class must and will implement the highlight() method. In consequence, as a programmer, knowing that a given class implements the highlightable interface lets you know that somehow it can be highlighted.

Ideally, a good interface should indicate the intended purpose of each of its methods as follows:

/**
 * An interface for all things that can be highlighted.
 */
public interface highlightable {

    /**
     * Implementations should make the subject stand out.
     */
    public void highlight();

}

so when a programmer is coding the implementation, it is clear what needs to be done.

Solution 3

Solution with Java 8:

Use java 8 default methods and change definition of your interface as

public interface a {
    public void m1();
    public void m2();
    default void m3(){
        // Provide your default imp
    }
}

Extending Interfaces That Contain Default Methods

When you extend an interface that contains a default method, you can do the following:

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.
  3. Redefine the default method, which overrides it.

Depending on your requirement, you can skip implementing this default method in some of your classes (like A) or you can override the default method in some other classes or delegate the implementation of this method to some other classes by redeclaring this method as abstract.

Solution with Java 7 or earlier version

Break the interface definition into two sub-interfaces.

public interface a {
    public void m1();
    public void m2();

}

public interface b{
  public void m3();
}

Now class A will implement only interface a and other classes can implement both interface a and interface b

Solution 4

Use abstract class instead of interface.

In this abstract class,

  1. Methods which are optional for Child class - provide implementation in your abstract class
  2. Methods which are mandatory for Child class - declare as abstract in your abstract class, child class must provide implementation
  3. Methods which Child class CAN NOT override- declare as final in your abstract class, and provide implementation

Example below:

abstract class MyClass {
    public void m1(){}; //class A can override, optional
    final public void m2(){}; //class A CANNOT override, has to use implementation provided here
    abstract public void m3(); //class A MUST override, mandatory
}

class A extends MyClass {

    @Override
    public void m3() { //mandatory

    }
}

Solution 5

You don't explain why you need this, so let me guess. Perhaps you need a quick way to stub out some functionality for a test. Or you just want something to compile so you can run some unit-tests and come back and finish the rest later. Or perhaps you suspect that some of the methods are superfluous and will never be called in your application.

This would cover all of the above:

public class A implements a {
    public void m3() {
        throw new UnsupportedOperationException("This functionality has not been implemented yet.");     
    }
}
Share:
21,700
Joe
Author by

Joe

Updated on January 16, 2020

Comments

  • Joe
    Joe over 4 years

    I have a interface like the below :

    public interface a {
        public void m1();
        public void m2();
        public void m3();
    }
    
    public class A implements a {
        public void m3() {
            // implementation code     
        }
    }
    

    I want to avoid implementation for the rest of the method. one way is to have all the methods without implementing in the class that tries to implement interface.

    How do I avoid this. Example code would help me understand better :)

  • jumping_monkey
    jumping_monkey about 2 years
    Good point about the Javadoc, even if you are the creator of the interface, you will forget why the method is there. I am writing this comment for myself so that it reminds me to always write Javadoc.