We use abstract classes if we need to keep an implemented method and method definitions while we use interfaces if we need to keep just the method definitions. But in java and C# we can use default methods and static methods to include implementations in interfaces. So what is the use of abstract classes if we can achieve what it is intended to do by using interfaces?
Asked
Active
Viewed 31 times
0
-
Does this answer your question? [When to use: Java 8+ interface default method, vs. abstract method](https://stackoverflow.com/questions/19998454/when-to-use-java-8-interface-default-method-vs-abstract-method) – jaco0646 Jul 28 '22 at 14:12
-
Also see additional answers to the query: https://stackoverflow.com/questions/tagged/java%2babstract-class%2binterface?tab=Votes – jaco0646 Jul 28 '22 at 21:47
1 Answers
1
There are some reasons to have abstract classes.
Abstract classes can inherit interfaces, but not vice versa
Abstract classes can inherit interfaces. However, interface cannot inherit abstract class:
public interface ABar { }
public abstract class Bar : IFoo { }
However, this is not eligible:
public interface IFoo : ABar
{
}
public abstract class ABar
{
}
Abstract classes can have constructors, fields (can hold state)
Abstract classes can have constructors, fields. By having fields, it is possible to hold state. However, interface cannot have them.
public abstract class ABar
{
public int test; // some state
public ABar()
{
}
}

StepUp
- 36,391
- 15
- 88
- 148