โ† Java
#4 most askedVery commonEasyCore Java & OOP

Abstract class vs interface

New to Core Java & OOP? Learn it from scratch first
Inheritance vs Composition โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

When would you use an abstract class instead of an interface in modern Java?

๐ŸŽค Answer it out loud, then check yourself

Say your answer as if the interviewer is listening (30โ€“90 seconds). Then reveal what a strong answer covers and tick what you said.

    โ˜• Java example ยท compiled and run to verify it works
    interface Shape { double area(); default String describe() { return "area " + area(); } }
    abstract class Base implements Shape {
        protected final String name;          // shared state needs a class
        Base(String name) { this.name = name; }
    }
    class Circle extends Base {
        final double r;
        Circle(double r) { super("circle"); this.r = r; }
        public double area() { return Math.round(Math.PI * r * r * 100) / 100.0; }
    }
    public class Main {
        public static void main(String[] args) {
            Shape s = new Circle(2);
            System.out.println(s.describe());
        }
    }
    
    Answer out loud first, then compare.
    Want to rehearse it?
    Answer this to the AI interviewer and get a verdict โ†’