Partial Interface Implementation

With only a small amount of time to search for such things, I could not see anything that covered my specific scenario.

I work with a third-party interface. Let's say for a moment it looks like this:

public interface interface1 { public String getVar1(); public void method1(); public void method2(); } 

I need to create several classes that use this interface, but the actual implementation of the method "method2" will be different for each, where "method1" and "getVar1" will always be the same code.

Is there a way to create an abstract base class that implements the interface, but only forces the implementation of "method2" to the child classes?

I tried

 public abstract @Override void method2 

but while this is “acceptable” during development in Netbeans (I don’t know if Eclipse works differently) at compile time, he complains that method2 must be implemented.

If this is not possible, rightly, I just need to check.

thanks

+4
source share
4 answers

You can do this by creating an abstract class that implements the interface. Any subclass of this abstract class will be required to implement any interface methods that are not yet defined.

 public abstract class AbstractInterface implements interface1 { @Override public String getVar1() { } @Override public void method1() { } } 
 public class Implementation extends AbstractInterface { @Override public void method2() { } } 

See: Java tutorial on abstract classes .

+14
source

You can create an abstract class that does not implement method2()

 public abstract class AbstractImplementation implements interface1 { public String getVar1() { // implementation .. } public void method1() { // implementation .. } // skip method2 } 

Then create some implementations:

 public class Implementation1 extends AbstractImplementation { public void method2() { // implementation 1 } } public class Implementation2 extends AbstractImplementation { public void method2() { // implementation 2 } } ... 
+2
source

You may have an abstract Class that provides an implementation for "method 1" and "getVar1", but decalre method2 as abstract in this class.

Now inherit this abstract superclass in all other classes!

how

 public interface interface1 { public void getVar1(); public void method1(); public void method2(); } public abstract class test implements interface1 { @Override public void getVar1() { // TODO Auto-generated method stub } @Override public void method1() { // TODO Auto-generated method stub } @Override public abstract void method2(); } public class test1 extends test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } @Override public void method2() { // TODO Auto-generated method stub } } 
0
source

In this case, you have 3 abstract methods in the interface that you can override one or two methods, and the other methods will remain abstract

 abstact class PartialImplementationDemo implements interface1 { @Override public void method1() { // override method1() here } // leave other methods as abstract abstract public void method2(); abstract public String getVar1(); } 
0
source

Source: https://habr.com/ru/post/1500278/


All Articles