-1

I have an Inreface say

public interface myInterfacy {

    String kilogramToGram();

    Long litresTomiliLitres();

    String inchesToMillimeters();

    String ouncesToGrams();

}

I need to have multiple implementaton of this interface but I want the partial implementation of this inteface on different implementation, As:

public class A implements myInterfacy {

public String kilogramToGram(){
  //code
};
I don't want to give the definition of other methods.
}

public class B implements myInterfacy {

Long litresTomiliLitres(){
  //code
};
I don't want to give the definition of other methods.
}

I thought that I can di it via using an abstract class, but I wonder If any other good approach is possible.

ABC
  • 4,263
  • 10
  • 45
  • 72
  • You must implement all methods in the interface, unless they are `default` ([introduced in Java 8](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html)) - methods that have a *default* implementation. If you want to have abstract class, then you can provide a default implementation as you said. – Maroun Feb 16 '16 at 07:44
  • If classes `A` and `B` are supposed to be instantiatable, then why have interface with 4 methods, if only 1 gets implemented? – Andreas Feb 16 '16 at 07:44
  • Instead of inheritance, use composition to provide `class A` with an implementer (in its constructor or otherwise) for the methods that you don't want to implement in class A itself. – Marjan Venema Feb 16 '16 at 10:42

3 Answers3

1

The answer is relatively simple but has many options.

You could

  • Make a number of partial interfaces and the one that "does it all" implements them all (not great)
  • You could make a number of "dummy" interfaces which throw an exception of unimplemented functionality. So, every proxy class would implement the full interface but throw runtime errors on unsupported methods (also not great)
  • Simply do nothing - literally. Implement the full interface and provide empty bodies (also really not great)

Or, you could encapsulate the functionality with a specific proxy to provide the given functionality.For example,

class FullyFunctional {
    public void foo() {...}
    public void bar() {...}
}

class PartiallyFunctional {
    FullyFunctional ff; 

    public PartiallyFunctional(FullyFunctional ff) {
        this.ff = ff;
    }

    // No foo...
    public void bar() { ff.bar(); }
}
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
0

One way to do this, is with a convenience base class. This is however not really a good idea, because you won't get compile type checking to help ensure that you don't call unimplemented method.

public interface Converter {
    public String kilogramToGram();
    public long litresTomiliLitres();
    public String inchesToMillimeters();
    public String ouncesToGrams();
}
public abstract class AbstractConverter implements Converter {
    @Override
    public String kilogramToGram() {
        throw new UnsupportedOperationException();
    }
    @Override
    public long litresTomiliLitres() {
        throw new UnsupportedOperationException();
    }
    @Override
    public String inchesToMillimeters() {
        throw new UnsupportedOperationException();
    }
    @Override
    public String ouncesToGrams() {
        throw new UnsupportedOperationException();
    }
}
public final class A extends AbstractConverter {
    @Override
    public String kilogramToGram() {
        //code
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
-1

Follow interface-segregation-principle

  1. Divide fat interface into granular small interfaces
  2. Implement only require interface

One extreme case: I will declare four interfaces for four methods

public interface IKGToGram {

    String kilogramToGram();
}
public interface ILitersToMilliLeters{

    Long litresTomiliLitres();
}
public interface IInchesToMilliMeters{

    String inchesToMillimeters();
}
public interface IOunceToGrams{
    String ouncesToGrams();

}

Now you can implement whatever interface set you want to.

Have a look at explanation about interface segregation concept:

Interface Segregation Principle- Program to an interface

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • @Downvoter, have a re-look into ISR principle and applicability to OP problem. A class should not implement more or less. It's better than using Adapter with composition or inheritance. – Ravindra babu Feb 17 '16 at 01:59