not implementing all of the methods of interface. is it possible?

JavaInterfaceAbstract Class

Java Problem Overview


Is there any way to NOT implement all of the methods of an interface in an inheriting class?

Java Solutions


Solution 1 - Java

The only way around this is to declare your class as abstract and leave it to a subclass to implement the missing methods. But ultimately, someone in the chain has to implement it to meet the interface contract. If you truly do not need a particular method, you can implement it and then either return or throw some variety of NotImplementedException, whichever is more appropriate in your case.

The Interface could also specify some methods as 'default' and provide the corresponding method implementation within the Interface definition (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). These 'default' methods need not be mentioned while implementing the Interface.

Solution 2 - Java

The point of an interface is to guarantee that an object will outwardly behave as the interface specifies that it will

If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.

Solution 3 - Java

We can override all the interface methods in abstract parent class and in child class override those methods only which is required by that particular child class.

Interface

public interface MyInterface{
    void method1();
    void method2();
    void method3();
}

Abstract Parent class

public abstract class Parent implements MyInterface{
@Override
public void method1(){
            
}
@Override
public void method2(){
            
}
@Override
public void method3(){
            
}
}

In your Child classes

public class Child1 extends Parent{
    @Override
    public void method1(){
                
    }
}




public class Child2 extends Parent{
    @Override
    public void method2(){
                    
    }
}

Solution 4 - Java

I asked myself the same question, and then learned about Adapters. It solved my problem, maybe it can solve yours. This explains it very well : https://blogs.oracle.com/CoreJavaTechTips/entry/listeners_vs_adapters

Solution 5 - Java

You can do that in Java8. Java 8 introduces “Default Method” or (Defender methods) new feature, which allows a developer to add new methods to the Interfaces without breaking the existing implementation of these interfaces.

It provides flexibility to allow Interface define implementation which will use as default in the situation where a concrete Class fails to provide an implementation for that method.

interface OldInterface {
	public void existingMethod();

	default public void DefaultMethod() {
		System.out.println("New default method" + " is added in interface");
	}
}
//following class compiles successfully in JDK 8
public class ClassImpl implements OldInterface {
	@Override
	public void existingMethod() {
		System.out.println("normal method");
		
	}
	public static void main(String[] args) {
		ClassImpl obj = new ClassImpl ();
		// print “New default method add in interface”
		obj.DefaultMethod(); 
	}
}

Solution 6 - Java

Define that class as an abstract class. However, you must implement those unimplemented methods when you want to create an instance of it (either by using a subclass or an anonymous class).

Solution 7 - Java

It is possible and it is easy. I coded an example.

All you have to do is inherit from a class that does implement the method. If you don't mind a class that is not instantiable, then you can also define an abstract class.

Solution 8 - Java

If you want an instantiable class, it is not possible. You may try to define an abstract class, though.

Solution 9 - Java

If you try to implement an interface and you find yourself in a situation where there is no need to implement all of them then, this is a code smell. It indicates a bad design and it violates Liskov substitution principle. Often this happens because of using fat interface.

Also sometimes this happens because you are trying to implement an interface from an external dependency. In this case, I always look inside the source code to see if there is any implementation of that interface which I can either use it directly or subclass it and override methods to my needs.

Solution 10 - Java

We can use Adapter classes ,which reduces complexcity by not making mandatory to implement all the methods present in the interface

Adapter class is a simple java class that implements an interface with only EMPTY implementation . Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method

ex--- instead of implementing Servlet(I) if we extends GenericServlet(AC) then we provide implementation for Service()method we are not require to provide implementation for remaining meyhod..

Generic class Acts as ADAPTER class for Servlet(I).

Solution 11 - Java

yes possible below shown is the way

interface Test {
   void m() throws NullPointerException;
}
class Parent {
   // Parent class doesn't implements Test interface
   public void m() {
      System.out.println("Inside Parent m()");
   }
}
class Child extends Parent implements Test {
}
public class Program {
   public static void main(String args[]) {
      Child s = new Child();
      s.m();
   }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionApurva PatilView Question on Stackoverflow
Solution 1 - JavaMike ClarkView Answer on Stackoverflow
Solution 2 - JavaSam I am says Reinstate MonicaView Answer on Stackoverflow
Solution 3 - JavaPravin DivraniyaView Answer on Stackoverflow
Solution 4 - JavaeigenaarView Answer on Stackoverflow
Solution 5 - JavaShubham ChopraView Answer on Stackoverflow
Solution 6 - JavaEng.FouadView Answer on Stackoverflow
Solution 7 - JavaemoryView Answer on Stackoverflow
Solution 8 - JavaSJuan76View Answer on Stackoverflow
Solution 9 - JavaartronicsView Answer on Stackoverflow
Solution 10 - JavaNaveen PatkeView Answer on Stackoverflow
Solution 11 - Javavemuri SAI ARAVINDView Answer on Stackoverflow