Java Interfaces Methodology: Should Every Class Implement an Interface

Java Interfaces Methodology: Should every class implement an interface?

You don't need to create an interface if you are not going to use it.

Typically you need an interface when:

  • Your program will provide several implementations for your component. For example, a default implementation which is part of your code, and a mock implementation which is used in a JUnit test. Some tools automate creating a mock implementation, like for instance EasyMock.
  • You want to use dependency injection for this class, with a framework such as Spring or the JBoss Micro-Container. In this case it is a good idea to specify the dependencies from one class with other classes using an interface.

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

if an interface were inherited by only one class, it would be useless

I agree, but you can read a lot of discussion on this topic in Java Interfaces Methodology: Should every class implement an interface?

when an interface is inherited by multiple classes, it makes no difference unless it is used for polymorphism

I agree, it would be silly to implement an interface and then bypass it by invoking its methods directly on the concrete class. The purpose of implementing an interface is to have its abstract methods called, which is polymorphism in action. Since polymorphism is the primary tool of Object Oriented Programming, you could take this statement a step further and say that OOP makes no difference unless it is used for polymorphism.

the only thing that makes implementation different from extension is multiple inheritances

No, the primary difference between these Java constructs is that interface implementation facilitates stateless inheritance whereas class extension facilitates stateful inheritance. Java happens to support multiple stateless inheritance and single stateful inheritance. This dichotomy may be considered a mistake.

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

In practice, yes. You can find interfaces used for other purposes such as constant interfaces or marker interfaces. Whether these practices are advisable has been debated.

interface method that does not make sense for all implementing classes

Throw an UnsupportedOperationException when there is no reasonable implementation and you know that the method is not getting called.

Should every @Service class in a SpringBoot app implement an interface

This pattern was indeed used quite a bit in quite some tutorials and online resources but I'd argue it's quite outdated. Back in the XML days, you didn't have such a thing as a configuration class so the main way of configuring a bean was an XML entry, with public setters for the collaborators. As you didn't want to expose that in your API, having an interface that declares your public API and the implementation taking care of it made sense.

With modern Spring, this view of the world is totally outdated and there's certainly not a strong desire on our side for such a paradigm. Constructor injection is the norm now, and with that, the separation between an interface and its sole implementation is not very useful.

One could argue that they don't want any Spring annotations to surface on public APIs so this as still a use. That said, if you use construction injection and explicit bean registration, you don't need any annotations for dependency injection. If you're feeling strongly about this and use additional aspects such as transaction or caching, then yes you'll have to create an interface if you want to hide those annotations from your public API.

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

Should methods in a Java interface be declared with or without a public access modifier?

The JLS makes this clear:

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

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.

How are Java interfaces used?

Interface in java is like a contract with the implementing class. When you implement an interface you have to make sure that either you implement all the methods in the interface or make your class abstract. Lets take a real like analogy.

You are creating a Car. Now as you know there can be a number of cars out there i.e. Mercedes, BMW, Audi and so on. You want to make sure that each Car should contain changeGear(), hasWheels(), hasDoors() methods in them, so how would you force this criteria on all possible cars. Simply create an interface named Car that has all these methods in it like

public interface Car{
boolean changeGear();
boolean hasWheels();
boolean hasDoors();
}

Now every class that implements this interface has to implement all these methods. So, in future if you create a Ferrari class implementing Car interface it has to abide by this contract.

Edit: A thing to note here is that all the classes that implement the interface are not restricted to use only the methods from the interface. It is just like the implementing class has to make sure that it at least implements the methods from interface. Like in our example Ferrari class can very well implement the isSportCar() method which is only contained in Ferrari class.



Related Topics



Leave a reply



Submit