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 :)
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 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.
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.
If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.
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:
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With