When To Use Interfaces
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package. The disadvantage is every method in the interface must be public. You might not want to expose everything.
When To Use Abstract classes
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.
When to Use Both
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.
Answer from DivineDesert on Stack OverflowCan someone please give me a simple explanation of the difference between the two? I've done some Googling, but can't find a clear explanation that I can understand. Also, what are the real-world difference/impacts of using an interface vs. a class?
design patterns - Abstract class vs Interface in Java - Stack Overflow
Under what circumstances should I use an interface in Java instead of a class in system design? - Stack Overflow
coding style - Are there advantages to using an interface, rather than a class, for a Java application? - Software Engineering Stack Exchange
Can someone explain the difference(s) between an interface and class?
Videos
When To Use Interfaces
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package. The disadvantage is every method in the interface must be public. You might not want to expose everything.
When To Use Abstract classes
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.
When to Use Both
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.
reiterating the question: there is any other scenario besides these mentioned above where specifically we require to use abstract class (one is see is template method design pattern is conceptually based on this only)
Yes, if you use JAXB. It does not like interfaces. You should either use abstract classes or work around this limitation with generics.
From a personal blog post:
Interface:
- A class can implement multiple interfaces
- An interface cannot provide any code at all
- An interface can only define public static final constants
- An interface cannot define instance variables
- Adding a new method has ripple effects on implementing classes (design maintenance)
- JAXB cannot deal with interfaces
- An interface cannot extends or implement an abstract class
- All interface methods are public
In general, interfaces should be used to define contracts (what is to be achieved, not how to achieve it).
Abstract Class:
- A class can extend at most one abstract class
- An abstract class can contain code
- An abstract class can define both static and instance constants (final)
- An abstract class can define instance variables
- Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
- Adding a new method to an abstract class has no ripple effect on extending classes
- An abstract class can implement an interface
- Abstract classes can implement private and protected methods
Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented.
A good place to look at would be the collections framework.
java.util.List //interface
java.util.ArrayList //Concrete class
java.util.LinkedList //Concrete class
So you can write code like this:
List l = new ArrayList();
l.add(..)
//do something else.
If in future you want to change the implementation with say LinkedList or you own AwesomeList which implements List interface, all you have to do is change the very first line to:
List l = new MyAwesomeList();
or
List l = new LinkedList();
The rest of the code would follow through.
Use interfaces to define an application programming contract (blueprint, interface) which "3rd-party" vendors have to fully adhere and implement. This way the end users can just code against the API contract and easily switch of the concrete implementation "under the hoods" without changing the code.
The JDBC API is an excellent example. It exists of almost only interfaces. The concrete implementations are provided as "JDBC drivers". This enables you to write all the JDBC code independent of the database (DB) vendor. You can just change the JDBC driver without changing any line of Java code (except of any hard coded DB-specific SQL code) whenever you'd like to switch of DB vendor.
Another example is the Jakarta EE API, it also consists of pretty much interfaces and abstract classes. The concrete implementations are provided as "Jakarta EE application servers" such as WildFly, GlassFish, Liberty, TomEE, etc. This enables you to deploy the web application archive (WAR) to whatever application server you want without changing any line of Java code (except of any server-specific XML configuration files) whenever you'd like to switch of runtime.
See also:
- In simplest terms, what is a factory?
Until relatively recently, you could not define a concrete method on any interface in Java. Class was the only option. It never occurred to me to define main on an interface since that has been allowed (1.8, IIRC). One minute ... OK yes, that works.
It seems you can use an interface to hold your main method if you like. As for advantages, I don't see any, really. static variables and methods and variables in Java are effectively procedural. That is, they can't be overridden, only hidden and therefore do not support sub-typing polymorphism. In essence, public static methods are globally defined (with some esoteric caveats.)
The only thing that would change if you define your main method on an interface instead of a class is that anything else defined in that 'type' would need to be public. You would also not be able to define a constructor on that type. If all your application classes do is hold a single main method, then it really doesn't matter which you choose. There's literally no difference (to my knowledge) between a public static method defined on an interface and one defined on a class. Whatever floats your boat, I suppose.
But to join the chorus: you are overthinking things a bit. The main method is basically a convention. When the JVM starts, it needs an entry point. That was the one James Gosling chose. Ultimately the answer is, defining main on a class was the only option for most of Java's history and there's no clear reason to use an interface but if you prefer it, I don't see a problem with it aside from the fact that it will be surprising to people.
A class represents an actual entity in code. It provides the implementation for your code and the ability to define implementations on an interface is actually relatively new (e.g. default implementations). Yes you can have static members on an interface but typically the static main method is just there for some initialization to get an actual instance of your application up and running. By using an interface, you're vacating all the abilities defining and working with members that don't exist on an interface. An interface is intended to provide the definition of the interaction pattern and not it's implementation. Default methods implementations are a modern convenience and static members are very uncommon. They should be named as adjectives as they describe the behavior the class provides.
In Java, you're supposed to prefer interaction classes via the interface so that implementations can be easily changed without having to change all the references to the functionality. You can also provide more granularity with interfaces, only exposing clients of your classes to what they need.
Some recommended reading to help understand the role of interfaces and how to leveraged them effectively:
https://dev.to/kylec32/effective-java-tuesday-prefer-interfaces-to-abstract-classes-21cn https://stackify.com/interface-segregation-principle/