Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 468
Joe Avatar asked Dec 12 '13 10:12

Joe


People also ask

How can we avoid implementing all methods interface in Java?

However, you can avoid this by following the below approach: Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.

What happens if we provide the implementation of a method in an interface?

What will happen if we provide concrete implementation of method in interface? Explanation: The methods of interfaces are always abstract. They provide only method definition. 8.

Do All methods in an interface have to be implemented?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

What will happen if we are not implementing all the methods of an interface in class which implement an interface?

If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.


2 Answers

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

like image 129
Ravindra babu Avatar answered Sep 28 '22 09:09

Ravindra babu


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.

like image 40
Samuel Avatar answered Sep 28 '22 08:09

Samuel