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

送分小仙女□ 提交于 2019-11-27 03:12:01
Mike Clark

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.

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.

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(){

    }
}

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

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

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

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.

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(); 
    }
}

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!