Difference between Abstract Class and Interface in Java — Java Technical Interview Questions & Answers
Difference between Abstract Class and Interface in Java
Difference between Abstract Class and Interface in Java
- This is a favourite interview question, so learn it like a class, not like a slogan. Abstract class = a partial parent in a family. Interface = a can-do contract. Shape is a family: circle and rectangle are shapes. Drawable is a capability: something that can draw. A Circle is a Shape, and it can also be Drawable. That one picture is enough if you can explain it slowly.
- You cannot do new Shape() if Shape is abstract. You cannot do new Drawable() on a plain interface. A class extends only one abstract parent. A class can implement many interfaces. Say that first: one parent class, many interfaces. Interviewers wait for that line. Then they ask why.
- Why? Abstract class can keep real fields and real methods — colour, x, y, a helper print() that every child reuses. Interface is mainly “you must be able to do this”: draw(), compareTo(). Modern Java has default methods on interfaces, yes. Still don’t say they are the same. Intent is different: family shared code vs capability contract.
- Let’s take this on the board. abstract class Animal { abstract void sound(); } interface Pet { void play(); } class Dog extends Animal implements Pet { void sound() { print Bark; } public void play() { print Fetch; } }. Then Dog d = new Dog(); d.sound(); d.play();. Output: Bark then Fetch. Dog is an Animal (IS-A family) and also a Pet (CAN-DO). One tiny program, both ideas.
- How to choose in a project. Related types that share code → abstract class. Circle and Rectangle both need colour and area() — Shape. Unrelated types that share an ability → interface. A printer and a screen both can implement Drawable. A car and a bird are not one family. Don’t force Flyable on a car.
- Answer with a small table if they ask “difference”: inheritance count, shared code/fields, purpose (IS-A vs CAN-DO), keyword (extends vs implements). Then give Shape vs Drawable. Don’t recite ten Java version notes unless they ask about default methods.
Shape (abstract class)
colour, area(), print()
│ extends
▼
Circle
│ implements
▼
Drawable (interface)
draw()
abstract class = family + shared code
interface = can-do contractTechnical Interview Tip: Table: one parent vs many interfaces; shared code vs contract; IS-A vs CAN-DO. Then one example: Shape vs Drawable.
Code Examples for Difference between Abstract Class and Interface in Java
Example
abstract class Animal {
abstract void sound();
}
interface Pet {
void play();
}
class Dog extends Animal implements Pet {
void sound() { System.out.println("Bark"); }
public void play() { System.out.println("Fetch"); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.play();
}
}Difference between Abstract Class and Interface in Java — output: Bark then Fetch. Dog extends Animal (one family, IS-A) and implements Pet (can-do). That is the real difference in one program.
How to Explain Difference between Abstract Class and Interface in Java to an Interviewer
What is the difference between abstract class and interface?
Abstract class = family / shared code Interface = can-do contract One parent class vs many interfaces
How many abstract classes can a Java class extend?
One
How many interfaces can a class implement?
Many
When do you pick an abstract class?
Related types share code IS-A family Example: Shape
When do you pick an interface?
Capability / can-do Unrelated types can share an ability Example: Drawable, Comparable
Can Circle use both?
Yes extends Shape implements Drawable