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 Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › differences-between-interface-and-class-in-java
Java Class vs Interfaces - GeeksforGeeks
A class can extend another class and implement an interface, while an interface can only extend another interface, not implement a class.
Published   October 21, 2025
🌐
Reddit
reddit.com › r/javahelp › eli5: difference between classes and interfaces?
r/javahelp on Reddit: ELI5: Difference between classes and interfaces?
December 7, 2013 -

Can 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?

Top answer
1 of 5
6
A class is a blueprint of what an object is. An interface is a contract saying what a class will do. Any classes which do the same thing can implement the same interfaces, and a class can implement more than one interface if it does more than one thing. In java, everything you create must have a class, and that class may implement zero or more interfaces. You use an interface when you support or require a specific kind of behavior. For example, java has an interface called Comparable. This interface is a contract that stipulates two things: first, that any object implementing Comparable has a method like: public int compareTo(T o); // don't worry about the T And second, that the int you return has a specific meaning. If your object should come before o in a sorted list, the return value should be negative. If it should come after o, the return value should be positive, and if they are equal, it should be 0. This allows anyone who creates a sorting method to base it on the interface, without knowing ahead of time which classes will call the sorting method. If you created a brand new class and implemented Comparable, you could pass an array of YourClass[] to Arrays.sort, and it would just work. It doesn't matter if your class is Students, Cars, SuperMessyData or whatever -- the sorter will only use the parts of your instance defined by the interface.
2 of 5
3
u/Uncle_DirtNap hit it right on the head. Classes contain 'methods'. Interfaces designate which methods you MUST have. For example, let's say we have two classes. 'Dog' 'Goldfish' A Dog may contain methods like.. giveBirth() doMammalStuff() and a Goldfish may contain methods like.. layEggs() doFishStuff() Now a dog and a goldfish are two very different animals, but they have at least one thing in common. They can both move. A dog runs while a fish swims but that doesn't matter because it's movement. If we had a game with air/earth/water monsters it'd be silly to have fly()/run()/swim() methods when we could just have one method move(). This is where interfaces come in. interface Move{ move(); } As you can see, we simply declared a method. It has no implementation and it shouldn't. All we are saying is that whoever implements this interface MUST have the method 'move()'. This allows us to do something like this class Dog implements Move{ .. implement class .. public void move(){ run(); //moves dog 1 space forward on ground } .. implement class .. } class Goldfish implements Move{ .. implement class .. public void move(){ swim(); //moves fish 4 spaces forward in water } .. implement class .. } class GameLoop{ public void makeEverythingMove(){ ArrayList animals = new ArrayList(); animals.add(new Goldfish()); animals.add(new Dog()); for(Move animal : animals){ animal.move(); } } } If you didn't understand that, basically if a Class implements an Interface, it can then be CAST as that Interface to perform the functions within the Interface. ALL animals that implement the interface 'Move' can be stored in List. Then if you were to iterate over that List you can call 'move()' on every single class because you know FOR SURE that the class has a 'move()' method. Real life example Let's say there is an interface 'Calculus' that shows whether someone understands calculus. All engineers, physicists, computer scientists, etc could implement interface 'Calculus'. In this case, you could pool all those professions into a room and instead of differentiating them by their profession, you just call them all 'Calculus'. Because they all implement that interface, you could ask any single person in that room to derive an equation for you and they could do it because they know Calculus. The different professions are classes while 'Calculus' is an interface.
Discussions

design patterns - Abstract class vs Interface in Java - Stack Overflow
I was asked a question, I wanted to get my answer reviewed here. Q: In which scenario it is more appropriate to extend an abstract class rather than implementing the interface(s)? A: If we are using More on stackoverflow.com
🌐 stackoverflow.com
Under what circumstances should I use an interface in Java instead of a class in system design? - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
coding style - Are there advantages to using an interface, rather than a class, for a Java application? - Software Engineering Stack Exchange
But doing so allows use to use the class keyword only for factories that instantiate objects, and to use interface for collections of (static) methods, since the complaint about traditional Java application classes is that they really existed only to "house" the main method. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 20, 2022
Can someone explain the difference(s) between an interface and class?
This kind of question might indicate you are a young programmer, at least in Java, so, even if you had the best answers from other readers, I think I might add a bit of clarity, discussing not what a class and an interface are, but why we have both. In my experience, understanding the why is prerequisite for understanding the what. The why is this: classes implement inheritance, to allow for specialization, interfaces describe aspects of behaviour to allow composition. The first is described in all the animal/mammal/dog examples you see around (and by the way, two of them are abstract, but this is an other topic). The second can be described like this. You might have Readable objects on which you can call a read() method. There could also be Writable objects with a write() method. And, of course, there could be objects who have both. Thinking that Readable and Writable objects should derive from a common ancestor, different from what is just Readable or Writable is ridiculous. Not just is a superposition of definitions, but also restricts anything that is Readable to be just that, or a specialization. These are instead marginal aspects of what these objects might be, like a customer account, they are just just an aspect or a decoration. Interfaces allow tight specifications of this behaviour to allow composition. Some other languages solved the problem in a different way. Some, like C++ have multiple inheritance, a very complicated issue to solve. Some, like Go, have interfaces, but not class inheritance, hinting that composition and specification of contracts is more valued than inheritance itself in a simple functional language closer to C than to Java. More on reddit.com
🌐 r/javahelp
21
27
December 1, 2019
🌐
TutorialsPoint
tutorialspoint.com › differences-between-interface-and-class-in-java
Differences between Interface and class in Java
December 7, 2023 - Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Top answer
1 of 15
97

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.

2 of 15
35

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:

  1. A class can implement multiple interfaces
  2. An interface cannot provide any code at all
  3. An interface can only define public static final constants
  4. An interface cannot define instance variables
  5. Adding a new method has ripple effects on implementing classes (design maintenance)
  6. JAXB cannot deal with interfaces
  7. An interface cannot extends or implement an abstract class
  8. 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:

  1. A class can extend at most one abstract class
  2. An abstract class can contain code
  3. An abstract class can define both static and instance constants (final)
  4. An abstract class can define instance variables
  5. Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
  6. Adding a new method to an abstract class has no ripple effect on extending classes
  7. An abstract class can implement an interface
  8. 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.

Top answer
1 of 11
102

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.

2 of 11
45

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?
🌐
W3Schools
w3schools.com › java › java_interface.asp
Java Interface
Interface methods do not have a body - the body is provided by the "implement" class · On implementation of an interface, you must override all of its methods · Interface methods are by default abstract and public · Interface attributes are by default public, static and final · An interface cannot contain a constructor (as it cannot be used to create objects) 1) To achieve security - hide certain details and only show the important details of an object (interface). 2) Java does not support "multiple inheritance" (a class can only inherit from one superclass).
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
When to use abstract classes vs. interfaces in Java | InfoWorld
December 6, 2023 - It’s a good idea to use an abstract class when you need to implement mutable state. As an example, the Java Collections Framework includes the AbstractList class, which uses the state of variables. In cases where you don’t need to maintain the state of the class, it’s usually better to use an interface.
Find elsewhere
🌐
BYJUS
byjus.com › gate › difference-between-abstract-class-and-interface-in-java
Difference between Abstract Class and Interface in Java
February 16, 2024 - An interface is a sketch that is useful to implement a class. The methods used in the interface are all abstract methods. The interface does not have any concrete method.
Top answer
1 of 4
2

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.

2 of 4
1

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/

🌐
Oracle
docs.oracle.com › javase › tutorial › java › IandI › interfaceDef.html
Defining an Interface (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.
🌐
Medium
harsh05.medium.com › abstract-classes-vs-interfaces-in-java-when-and-how-to-use-them-5ca5d5c825b5
Abstract Classes vs Interfaces in Java: When and How to Use Them | by @Harsh | Medium
October 10, 2024 - Along the way, I also explored how multiple inheritance, although not directly supported in Java, can be achieved using interfaces. ... An abstract class in Java is a class that cannot be instantiated on its own. Instead, it serves as a blueprint for other classes that extend it.
🌐
Medium
medium.com › @jschapir › today-i-learned-abstract-classes-vs-interfaces-in-java-3bfd658c54a0
Today I Learned: Abstract Classes vs. Interfaces in Java | by Jonathan Schapiro | Medium
March 29, 2016 - See the various comments below, the discussion is interesting.” — Jay · Key Points: Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes.
🌐
Baeldung
baeldung.com › home › java › core java › using an interface vs. abstract class in java
Using an Interface vs. Abstract Class in Java | Baeldung
June 24, 2025 - We can have instance and static initialization blocks in an abstract class, whereas we can never have them in the interface. Abstract classes may also have constructors which will get executed during the child object’s instantiation. Java 8 introduced functional interfaces, an interface with a restriction of no more than one declared abstract method.
🌐
Quora
quora.com › How-can-you-explain-the-concept-of-Java-interfaces-and-compare-them-with-abstract-classes-using-real-world-scenarios
How to explain the concept of Java interfaces and compare them with abstract classes using real-world scenarios - Quora
Answer (1 of 3): One example of how an Interface is useful, is that an Interface can apply to classes which have no direct relationship with one another. An Interface could be thought of like a “feature” that a class provides.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › difference between class and interface
Difference Between Class and Interface - Shiksha Online
September 18, 2023 - For example, when you turn on a ... to that of the class. In the Java programming language, the interface is used to inherit more than one class at a time....
🌐
Hero Vired
herovired.com › home › learning-hub › blogs › difference-between-abstract-class-and-interface
Difference Between Abstract Class and Interface in Java
Interfaces, on the other hand, can only have abstract methods, do not provide implementations, and support multiple inheritance through implementation. This article will help you understand the key differences between abstract class vs interface, two of the main building blocks of the Java programming language.
🌐
Reddit
reddit.com › r/javahelp › can someone explain the difference(s) between an interface and class?
Can someone explain the difference(s) between an interface and class? : r/javahelp
December 1, 2019 - You guest it, I’m new to java! Thank you for response it was really helpful! ... When I was reviewing the basics of object-oriented programming, one of the best explanations I heard about interfaces is to think of them as a *contract* for inheriting classes to follow.
🌐
DigitalOcean
digitalocean.com › community › tutorials › difference-between-abstract-class-and-interface-in-java
Difference between Abstract Class and Interface in Java | DigitalOcean
August 4, 2022 - Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations. Note that from Java 8 onwards, we can create default and static methods in interface that contains the method implementations.
🌐
Coderanch
coderanch.com › t › 755215 › java › Abstract-Class-Interface
Abstract Class Vs Interface (Beginning Java forum at Coderanch)
If you need to share implementation details, contain a class that contains the common functionality and COMPOSE that class, not inherit it. Then delegate interface methods to the proper class. Especially in Java where you only get 1 chance to get your hierarchy right.