- Home
- Interview Questions
- Java
- How do abstract classes differ from interfaces in Java?
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Abstract classes can have both abstract and concrete methods, while interfaces can only declare methods without implementing them.
Detailed explanation
In Java, both abstract classes and interfaces are tools for abstraction, but they serve different purposes and have distinct characteristics. An abstract class is a class that cannot be instantiated on its own and may contain both abstract methods (without implementation) and concrete methods (with implementation). This flexibility allows an abstract class to provide default behavior while still declaring methods that the subclasses must implement.
On the other hand, an interface is a reference type in Java that can only contain abstract method declarations and static final variables (constants). From Java 8 onwards, interfaces can also include default and static methods with implementations, but they still do not maintain state like classes do. Interfaces are primarily used to define a contract for what methods a class must implement, promoting polymorphism.
The choice between using an abstract class versus an interface typically depends on the design needs. When there is a shared behavior among a group of related classes, using an abstract class is suitable. But if a class is expected to have varied implementations across different hierarchies, interfaces are preferred for their flexibility.
Example
abstract class Shape { abstract void draw(); void paint() { System.out.println("Painting shape"); } } interface Drawable { void draw(); }Common mistakes
- Using an abstract class when an interface would be more appropriate and vice versa.
- Not realizing that an abstract class can have state (instance variables), while interfaces cannot.
- Overusing interfaces by defining abstract methods that can be encapsulated within a common base class.
Follow-up questions
- Can an abstract class implement an interface?
- What are default methods in interfaces and how do they function?
- Is it possible to extend multiple abstract classes in Java?
Interview tip: Clarify when to use abstract classes versus interfaces based on the design requirements of your application.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More hard Java questions
- How is a lambda different from an anonymous inner class in Java beyond shorter syntax? · 1 ans
- How does HashMap work internally, including collisions, resizing and treeification in Java 8? · 1 ans
- How do you implement a generic interface and its functions in Java? · 1 ans
- How does the JVM manage memory for Java applications? · 1 ans
- How do final variables differ from static final variables? · 1 ans
Related Questions
- How is a lambda different from an anonymous inner class in Java beyond shorter syntax? · Java · 1 ans
- What makes an interface a functional interface, and what does @FunctionalInterface actually do? · Java · 1 ans
- How does HashMap work internally, including collisions, resizing and treeification in Java 8? · Java · 1 ans
- What is the difference between HashSet, LinkedHashSet and TreeSet, and when would you use each? · Java · 1 ans
- How do you implement a generic interface and its functions in Java? · Java · 1 ans
- What are the constraints in Java generics and why are they used? · Java · 1 ans
- What are the advantages of using interfaces in Java? · Java · 1 ans
- How do final variables differ from static final variables? · Java · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)