Avoid implementing a method that is in the interface - java

I have an interface as shown 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 the implementation for the rest of the method. one way is to use all methods without an implementation in the class that is trying to implement the interface .

How to avoid this. Sample code will help me better understand :)

+7
source share
11 answers
 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 an abstract class, so you will not need to implement these methods in this class. But you must implement this method in a specific class

+5
source

TLDR

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

If you are writing an interface, then you are probably mistaken. If you want to implement only a subset of the methods, then you are probably better off using an abstract class.

In detail

The interface declares a behavioral contract. Its purpose is to force all implementing classes to implement all its methods, thus ensuring that the implementing classes conform to the contract.

For example, the following interface:

 public interface highlightable { public void highlight(); } 

declares that each implementing class must and will implement the highlight() method. Therefore, as a programmer, knowing that a given class implements a highlightable interface allows you to understand that you can highlight it in some way.

Ideally, a good interface should indicate the 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(); } 

therefore, when the programmer encodes the implementation, it becomes clear what needs to be done.

+5
source

Solution with Java 8:

Use java 8 default methods and change your interface definition as

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

Extension of interfaces containing default methods

When extending the interface that contains the default method, you can do the following:

  • Do not mention the default method, which allows the extended interface to inherit the default method.
  • Redeclare is the default method that makes it abstract.
  • Override the default method that overrides it.

Depending on your requirement, you can skip the default implementation of this 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, reusing this method as abstract.

Solution with Java 7 or earlier

Split an 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 only implement interface a , while other classes can implement as interface a and interface b

+3
source

Use an abstract class instead of an interface.

In this abstract class

  • Methods optional for the Child class - provide implementation in your abstract class
  • Methods that are required for the Child class — declare abstract in your abstract class, the child class must provide an implementation
  • Methods, the Child class CANNOT 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 } } 
+2
source

Besides what you said, the maximum you can do is make class A , abstract

 abstract class A implements a{ public void m3(){ // implementation code } 
+1
source

You should try Abstract class . Then you can decide what the method should implement, and what the method is not for.

 public abstract class A { public abstract void m1(); public void m2() { } public void m3() { } 

}

.

 public class B extends A{ @Override public void m1() { // you have to override m1 only } } 
0
source

By default, all methods in the public abstract interface . If you do not want to add an implementation for the inherited abstract method, you need to mark the class as abstract .

 public abstract class A implements a { // Compiler will not prompt you with an error message. // You may add or do not add implementation for a method m3() } 

Note: Abstract classes cannot be created. If you want to create an instance, add an empty implementation or create a subclass of it and implement the method there.

0
source

If you do not want to implement other interface methods, use a dummy class that implements these methods and inherits this class in your application. This is called an adapter pattern, which is widely used in swing adapter classes.

0
source

One solution to this is to create an abstract class that implements your interface method without code in them. Then your class A should extend the abstract class and override the correct method.

EDIT: this solution makes your interface a little useless if it is not used in a number of implementations.

0
source

You cannot do this before java 7. You must make an abstract class if you want to implement only some of the methods provided in the interface.

But at the moment, Java 8 is (until it starts) with the lambda feature. The specification states that you can provide a default implementation of the method in the interface itself.

eg

 public interface A { default void foo(){ System.out.println("Calling A.foo()"); } } public class Clazz implements A { } 

The code compiles even if Clazz does not implement the foo () method. The default foo () method is now provided by interface A.

0
source

You are not explaining why you need this, so let me guess. You might need a quick way to drown out some features for a test. Or you just want something to be compiled so that you can run some unit tests and come back and finish the rest later. Or perhaps you suspect that some of the methods are redundant and will never be called in your application.

This will cover all of the above:

 public class A implements a { public void m3() { throw new UnsupportedOperationException("This functionality has not been implemented yet."); } } 
0
source

All Articles