Not Implementing All of the Methods of Interface. Is It Possible

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

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.

Is there any way not to implement interface methods in Java?

The alternatives (to making your class abstract) are:

  1. Supply default implementation to the interface methods you don't want to implement in your class within the interface itself (requires Java 8 or higher).

    For example:

    public interface YourInterface {
    ...
    default boolean someMethod () {
    return false;
    }
    ...
    }
  2. Implement all the methods you don't want to implement with an empty body that throws an exception. This approach is common in the collections framework.

    For example:

    public class YourClass implements YourInterface {
    ...
    public boolean someMethod() {
    throw new UnsupportedOperationException();
    }
    ...
    }

Not implementing all the methods of an interface

The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() then if I apply the fix, I have many functions added

Comparator.reversed was introduced in Java 1.8 and it's a default method i.e. a method that you don't have to override.

It seems like you have your compliance level set to pre Java 1.8 (since Eclipse asks you to override reversed), while using Java 1.8 API (since Comparator has a reversed method).

Make sure you either change your API to 1.7 or change your compliance level to 1.8. (The latter option requires Eclipse Luna or better.)

More on Eclipse compliance level:
What is "compiler compliance level" in Eclipse?

How can I limit class not to implement all the methods of Interface in C#?

You can't avoid implementing all methods of the interface. If you inherit the interface you have to fulfil it.

In some situations some methods of an interface can't have a useful implementation for a specific class. After you have come to the conclusion that you should implement the interface despite this, there are some things that you can do:

  • You can implement a method as doing nothing. If the class already does what's expected without it, you can just accept the method call and silently do nothing.

  • You can throw a NotSupportedException, if some result is expected by calling the method, that the class can't fulfil. Naturally this should only be done if the method is not crucial for how the interface is supposed to be used.


Also, you have the choise of implementing interface members implicitly or explicilty. Implicitly is the normal way, where the member is visible both when the type of the reference is the interface and when it's the class.

To implement a member explicitly makes it only visible when the type of the reference is the interface, not when it's the class.

If the Multiply method is implemented explicitly in the class A (and the interface is named ICanCalc):

A obja = new A();
ICanCalc infa = new A();

infa.Multiply(); // works fine
obja.Multiply(); // gives a compiler error

However, the method is only hidden, you can still use it by simply casting the reference:

(ICanCalc)obja.Multiply(); // works fine

Interface methods in a class that does not implement it?

You can use an Anonymous Class that implements the interface:

For instance:

interface Foo<T> {
T foo();
}
class Bar<T> {
T t;
public Foo<T> bar() {
return new Foo<T>() { // <-- Anonymous class implementing `Foo`
public T foo() {
return t;
}
};
}
}

Execution:

Bar<String> b = new Bar<>();
b.t = "hello"; // with a setter in real life
Foo<String> f = b.bar();
f.foo(); // will return "hello"

The other option which I think would be the most common is to use a method that returns the interface, for instance the list interface has an iterator() method even though it itself doesn't implements the Iterator interface.

List<String> list = new ArrayList<>();
Iterator<String> stringIterator = list.iterator();

Here's the implementation

restriction of method while implementing an interface

You must implement all methods of your interface, unless the class implementing the interface is abstract.

If by restrict you mean that you want to predefine one or more of the methods, then you can use an abstract class instead of the interface. Abstract methods in an abstract class are methods that must be implemented by any class that extends the abstract class. Non-abstract methods are actually implemented in the abstract class, itself.

For example,

public abstract class MyClass 
{
abstract void methodOne();

void methodTwo()
{
//implementation code
}
}

public class MyOtherClass extends MyClass
{
void methodOne()
{
//implementation code
}
}

Here's a reference for Abstract Classes and Methods.

EDIT 1 (in response to comment):
I'm not really sure what you mean by a burden. All I'm saying is that if you want all methods to be implemented by the class, then use an interface.

If you only want some of the methods implemented by a class, then you can either use an abstract class instead of the interface

or

If it makes sense, have an abstract class implement the interface (partially) and then have the remainder of the methods implemented by whatever extends the abstract class.

Both approaches are reasonable. It depends on what you really need to do.

EDIT 2 (in response to additional comments):

Providing one user class with additional features seems like the perfect application for just extending the "normal user class" with a "super user" class that has the additional features. If you need an interface for the "super user" class, you can create an interface that extends the interface implemented by the "normal user" class.

Non mandatory method implementation of an interface in Java

For java 8+ you can mark method as default and provide it's implementation as empty body. Although in most cases you will want to separate interface, sometimes it is indeed useful. example would be:

interface A {
void first();
default void second(){
//throw new UnsupportedOperationException(); or do some default logic
}
}

In your implementing class you would only need to implement first method

Interfaces, what if not all implementations use all methods?

IMO, for the middle stage - it is OK to use NotImplementedException, until you finish implementing it.

However, as a permanentsolution - I believe it is a bad practice [in most cases].

Instead, I'd create an interface that contains behavior common to all implementing classes, and use subinterfaces to cluster them up for more specific behavior.

The idea is similar to java standard SortedSet, which extends a Set - we wouldn't want to regard Set as SortedSets and give a variable of this type a value of HashSet, instead we use a sub-interface, SortedSet for this purpose.



Related Topics



Leave a reply



Submit