avoid implementation of a method which is there in

2019-03-20 20:59发布

问题:

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

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



回答2:

The purpose of an interface is precisely to force all implementing classes to implement the methods of the interface, thus ensuring the class is compliant with the interface.

If you want to implement only a subset of the methods, maybe it's not an interface you should implement in the first place.



回答3:

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

    }
}


回答4:

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



回答5:

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     

         }


回答6:

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

}
}


回答7:

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.



回答8:

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.



回答9:

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.



回答10:

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.



回答11:

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