简体   繁体   中英

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

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

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

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

    }
}

Other than what you told, The maximum you can do is make you class A , abstract

abstract class A implements a{

     public void m3(){

         // implementation code     

         }

You should try Abstract class . Then you can decide what are the method should implement and what are the method not to.

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

}
}

By default, all the methods in a interface are public abstract . If you do not want to add implementation for a 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 instantiated. If you want instantiate it, either add blank implementation or create a subclass of it and implement the method over there.

If you don't want to implement other methods of an interface, use a dummy class which implement this methods and inherit that class in your application. This is called adapter pattern, which is widely used in the adapter classes of swing.

One solution to do that is to create an Abstract class implementing your interface's method without code in them. Then your class A should extend the abstract class and override the right method.

EDIT : this solution makes your interface a bit useless unless it's used in a number of implementations.

You can't to this till java 7. You have to make the class abstract if you want to implement only some of the methods provided in the interface.

But as of now Java 8 is coming (yet to launch) with the feature of lambdas. The specification says that you can provide a default implementation of a method in the interface itself.

for example

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

public class Clazz implements A {
}

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

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.");     
    }
}

The implementation of every method is mandatory if you are using interface. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM