I will give you an example first:

public interface LoginAuth{
   public String encryptPassword(String pass);
   public void checkDBforUser();
}

Suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods:

public class DBMySQL implements LoginAuth{
          // Needs to implement both methods
}
public class DBOracle implements LoginAuth{
          // Needs to implement both methods
}
public class DBAbc implements LoginAuth{
          // Needs to implement both methods
}

But what if encryptPassword() is not database dependent, and it's the same for each class? Then the above would not be a good approach.

Instead, consider this approach:

public abstract class LoginAuth{
   public String encryptPassword(String pass){
            // Implement the same default behavior here 
            // that is shared by all subclasses.
   }

   // Each subclass needs to provide their own implementation of this only:
   public abstract void checkDBforUser();
}

Now in each child class, we only need to implement one method - the method that is database dependent.

Answer from Vimal Bera on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-abstract-class-and-interface-in-java
Difference Between Abstract Class and Interface in Java - GeeksforGeeks
As we know that abstraction refers to hiding the internal implementation of the feature and only showing the functionality to the users. i.e., showing only the required features, and hiding how those features are implemented behind the scene. Whereas, an Interface is another way to achieve abstraction in Java. Both abstract class and interface are used for abstraction.
Published   July 23, 2025
Top answer
1 of 16
581

I will give you an example first:

public interface LoginAuth{
   public String encryptPassword(String pass);
   public void checkDBforUser();
}

Suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods:

public class DBMySQL implements LoginAuth{
          // Needs to implement both methods
}
public class DBOracle implements LoginAuth{
          // Needs to implement both methods
}
public class DBAbc implements LoginAuth{
          // Needs to implement both methods
}

But what if encryptPassword() is not database dependent, and it's the same for each class? Then the above would not be a good approach.

Instead, consider this approach:

public abstract class LoginAuth{
   public String encryptPassword(String pass){
            // Implement the same default behavior here 
            // that is shared by all subclasses.
   }

   // Each subclass needs to provide their own implementation of this only:
   public abstract void checkDBforUser();
}

Now in each child class, we only need to implement one method - the method that is database dependent.

2 of 16
228

Nothing is perfect in this world. They may have been expecting more of a practical approach.

But after your explanation you could add these lines with a slightly different approach.

  1. Interfaces are rules (rules because you must give an implementation to them that you can't ignore or avoid, so that they are imposed like rules) which works as a common understanding document among various teams in software development.

  2. Interfaces give the idea what is to be done but not how it will be done. So implementation completely depends on developer by following the given rules (means given signature of methods).

  3. Abstract classes may contain abstract declarations, concrete implementations, or both.

  4. Abstract declarations are like rules to be followed and concrete implementations are like guidelines (you can use it as it is or you can ignore it by overriding and giving your own implementation to it).

  5. Moreover which methods with same signature may change the behaviour in different context are provided as interface declarations as rules to implement accordingly in different contexts.

Edit: Java 8 facilitates to define default and static methods in interface.

public interface SomeInterfaceOne {

    void usualAbstractMethod(String inputString);

    default void defaultMethod(String inputString){
        System.out.println("Inside SomeInterfaceOne defaultMethod::"+inputString);
    }
}

Now when a class will implement SomeInterface, it is not mandatory to provide implementation for default methods of interface.

If we have another interface with following methods:

public interface SomeInterfaceTwo {

    void usualAbstractMethod(String inputString);

    default void defaultMethod(String inputString){
        System.out.println("Inside SomeInterfaceTwo defaultMethod::"+inputString);
    }

}

Java doesn’t allow extending multiple classes because it results in the “Diamond Problem” where compiler is not able to decide which superclass method to use. With the default methods, the diamond problem will arise for interfaces too. Because if a class is implementing both

SomeInterfaceOne and SomeInterfaceTwo

and doesn’t implement the common default method, compiler can’t decide which one to chose. To avoid this problem, in java 8 it is mandatory to implement common default methods of different interfaces. If any class is implementing both the above interfaces, it has to provide implementation for defaultMethod() method otherwise compiler will throw compile time error.

🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/learnjava › abstract class vs interface
r/learnjava on Reddit: Abstract class vs Interface
February 18, 2024 -

Hey everyone, thanks for dropping by, recently learnt about abstract class and interface at school.

In work experience, when would you use abstract class and interfaces vice versa, what’s the benefit over the other?

Interface, for me it is wonderful because it is a contract and we need to implement all the method, makes the class implements the interface much easier to understand.

What’s your view and experience on the two? Thank you for your time

Top answer
1 of 5
13
As a rule of thumb, I think of interfaces as "type definitions" and abstract classes as "partial implementation definitions." Technically, an interface and type definition could mean two different things, especially in Java but also depending on the language you use, but I find it useful to think of it this way because it entails that the interface specifies a contract but doesn't specify anything about how the thing is implemented. For that reason, it's more lightweight and easier to pass around and use. Abstract classes could technically be used "as" interfaces, but it generally carries a bit more baggage, as it entails that there is some kind of implementation going on that the user cares about. Usually, an abstract class defines some core functionality but leaves the rest to the subclass, so anyone looking to use an interface vs abstract class (if both are available) would only choose the abstract class as a type if they want something specifically from that class's implementation. Overall, interfaces are lightweight and simple to use, since they carry less baggage and can be substituted more easily, whereas abstract classes provide useful core implementations but restrict the user from using other classes that conform to that interface but don't inherit from that abstract class.
2 of 5
8
Abstract classes are great for defining a base entity that other classes should inherit from. Think like Executive extends Employee or CalculationSummaryCache extends SummaryCache Interfaces define behavior and not necessarily a parent type. You can have many unrelated classes implement a particular interface, whereas unrelated classes inheriting from an abstract class would be inappropriate. Think of an interface that requires its implementers produceCsvReport(). The implementing classes need not be related to each other at all, they just need to uphold the contract.
🌐
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 constructors but interfaces can’t have constructors. Abstract class have all the features of a normal java class except that we can’t instantiate it.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › IandI › abstract.html
Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
🌐
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
🌐
Unstop
unstop.com › home › blog › 10+ key differences between abstract class & interface in java
10+ Key Differences Between Abstract Class & Interface In Java // Unstop
January 8, 2025 - An abstract class in Java programming ... as a blueprint for other classes. An interface, on the other hand, is a reference type that primarily defines a contract with abstract methods, though from Java 8, it can also include ...
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › abstract-class-vs-interface-java
Abstract Class vs Interface Java: A Detailed Comparison
Below are the fundamental attributes ... modularity and the reuse of code. Abstract methods declare "what" needs to be done, leaving the "how" to the classes that implement the Interface....
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.

🌐
Great Learning
mygreatlearning.com › blog › it/software development › abstract class vs interface in java
Abstract Class vs Interface in Java
January 16, 2025 - Explore our Free Java Programming course to start your learning today · An interface in Java is a reference type, similar to a class, that can contain only abstract methods (methods without a body) and constants (public static final variables).
Top answer
1 of 6
38

In layman's terms:

Interfaces are for "can do/can be treated as" type of relationships.

Abstract ( as well as concrete ) classes are for "is a" kind of relationship.

Look at these examples:

class Bird extends Animal implements Flight;
class Plane extends Vehicle implements Flight, AccountableAsset;
class Mosquito extends Animal implements Flight;
class Horse extends Animal;
class RaceHorse extends Horse implements AccountableAsset;
class Pegasus extends Horse implements Flight;

Bird, Mosquito and Horse are Animals. They are related. They inherit common methods from Animal like eat(), metabolize() and reproduce(). Maybe they override these methods, adding a little extra to them, but they take advantage of the default behavior implemented in Animal like metabolizeGlucose().

Plane is not related to Bird, Mosquito or Horse.

Flight is implemented by dissimilar, unrelated classes, like Bird and Plane.

AccountableAsset is also implemented by dissimilar, unrelated classes, like Plane and RaceHorse.

Horse doesn't implement Flight.

As you can see classes (abstract or concrete) helps you build hierarchies, letting you inhering code from the upper levels to the lower levels of the hierarchy. In theory the lower you are in the hierarchy, the more specialized your behavior is, but you don't have to worry about a lot of things that are already taken care of.

Interfaces, in the other hand, create no hierarchy, but they can help homogenize certain behaviors across hierarchies so you can abstract them from the hierarchy in certain contexts.

For example you can have a program sum the value of a group of AccountableAssets regardless of their being RaceHorses or Planes.

2 of 6
13

You could deduce the answer logically since you seem to be aware of the differences between the two.

Interfaces define a common contract. Such as an interface called IAnimal, where all animals share functions such as Eat(), Move(), Attack() etc. While all of them share the same functions, all or most of them have a different way (implementation) of achieving it.

Abstract classes define a common implementation and optionally common contracts. For example a simple Calculator could qualify as an abstract class which implements all the basic logical and bitwise operators and then gets extended by ScientificCalculator, GraphicalCalculator and so on.

If you have common implementation then by all means, encapsulate the functionality in an abstract class to extend from. I have near 0 PHP experience, but i don't think you can create interfaces with non constant fields. If the fields are common between your instance classes then you are forced to use an Abstract class, unless you define access to them via getters and setters.

Also, there seems to be no shortage of results in Google.

🌐
BYJUS
byjus.com › gate › difference-between-abstract-class-and-interface-in-java
Difference between Abstract Class and Interface in Java
February 16, 2024 - An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class. Explore more differences between abstract class and interface in java.
🌐
Scaler
scaler.com › topics › java › difference-between-abstract-class-and-interface
Difference between Abstract Class and Interface in Java - Scaler Topics
February 16, 2022 - It serves as a template for other classes to extend and provides common functionality to its subclasses. On the other hand, an interface in Java is a reference type that contains only abstract methods, constants, and nested types.
Top answer
1 of 5
2

Note: please forgive the C# syntax, but the principle of the answer is the same for Java and C#.

Now when i started programming I noticed that in all subclasses i basically needed to do the exact same thing

Based on this, it seems like you think abstract classes are only allowed to declare abstract methods. This is not the case.

An abstract class is a class that cannot be instantiated directly (only its derivations can be instantiated). An abstract method is a method in an abstract class which must be implemented in the derived class.

But an abstract class can have non-abstract methods:

public abstract class Artikel
{
    public int ArtikelId { get; set; }

    public string SayHello()
    {
        return "Hi, I'm artikel " + ArtikelId;
    }
}

When you derive Artikel into subclasses, you do not need to repeat the method body of the SayHello method. Its body has been declared in the base class and can be used by all of the derived classes.

I thought of making Artikel not abstract and put an interface between Artikel and the other classes

Interfaces prevent the ability to create a common method body. If you were to use an interface:

public interface IArtikel
{
    string SayHello();
}

Then you will be required to implement this method separately in every class:

public class Book : IArtikel
{
    public string SayHello()
    {
        // custom book logic
    }
}

// And the same for all other derived classes.

It's also possible to make an seperate class which inherits from Artikel where I can put all the methods, but then there the methods would still be needed to made three times, one for each subclass right?

Don't take this the wrong way, but your attempts at solving this suggest you don't really master OOP. If this SeparateClass was created as another (4th) subclass from Artikel, how would you expect e.g. the Book class to rely on the methods found in SeparateClass?

Is it a bad design choice if I keep Artikel as abstract?

Keep Artikel abstract, but give it non-abstract methods (i.e. with method bodies) for each method that you are now copy/pasting between all of its subclasses.

2 of 5
1

You can have a base class as an abstract class which implements the Artikel interface. In the abstract class you can define the common implementation. Then you can derive LP, Book and Boardgame from that super class. In my opinion it is better to have a abstract class rather than copying the same code in all 3 sub classes.

🌐
Baeldung
baeldung.com › home › java › core java › interface with default methods vs abstract class
Interface With Default Methods vs Abstract Class | Baeldung
January 16, 2024 - An abstract class can declare instance variables, with all possible access modifiers, and they can be accessed in child classes. An interface can only have public, static, and final variables and can’t have any instance variables.
🌐
Javatpoint
javatpoint.com › difference-between-abstract-class-and-interface
Abstract vs Interface - javatpoint
Difference between Abstract class and Interface or difference between interface and abstract class in java or abstract class vs interface in abstraction with example of abstract class and example of interface,abstract class vs interface in java, example of abstract class and interface in java
🌐
TutorialsPoint
tutorialspoint.com › differences-between-abstract-class-and-interface-in-java
Differences between abstract class and interface in Java
Following are the important differences between abstract class and an interface. public class JavaTester { public static void main(String args[]) { Animal tiger = new Tiger(); tiger.eat(); Cat lion = new Lion(); lion.eat(); } } interface Animal { public void eat(); } class Tiger implements Animal { public void eat(){ System.out.println("Tiger eats"); } } abstract class Cat { abstract public void eat(); } class Lion extends Cat{ public void eat(){ System.out.println("Lion eats"); } }