Difference between abstract class and interface

๐Ÿ’ก Concept: Abstract Class vs Interface in Java

Abstract classes and interfaces define contracts and partial implementations but have distinct purposes and capabilities.

๐Ÿ“˜ Quick Intro

Abstract classes can have method implementations and state, while interfaces define method signatures (and default methods in Java 8+).

๐Ÿง  Analogy

Think of an abstract class as a partially built car frame, while an interface is the contract that guarantees the car has wheels, brakes, and lights.

๐Ÿ”ง Technical Explanation

  • Abstract class: Can have fields, constructors, concrete and abstract methods.
  • Interface: Only method signatures and static/default methods (Java 8+).
  • Classes can extend one abstract class but implement multiple interfaces.
  • Abstract classes support access modifiers; interfaces are implicitly public.
  • Abstract classes used for shared code; interfaces define capabilities.

๐ŸŽฏ Use Cases

  • โœ… Use abstract classes to share code among closely related classes.
  • โœ… Use interfaces to define capabilities for unrelated classes.
  • โœ… Use interfaces to achieve multiple inheritance.

๐Ÿ’ป Java Code Example


abstract class Vehicle {
    abstract void move();
    void start() {
        System.out.println("Vehicle started");
    }
}

interface Drivable {
    void drive();
}

class Car extends Vehicle implements Drivable {
    void move() {
        System.out.println("Car moves");
    }

    public void drive() {
        System.out.println("Car driving");
    }
}

โ“ Interview Q&A

Q1: Can a class extend multiple abstract classes?
A: No, Java allows only single inheritance for classes.

Q2: Can a class implement multiple interfaces?
A: Yes, without limitation.

Q3: What can abstract classes have?
A: Fields, constructors, and concrete methods.

Q4: What can interfaces contain?
A: Method signatures and default/static methods.

Q5: Are interface methods public?
A: Yes, implicitly.

Q6: Why use abstract classes?
A: To share common code among related classes.

Q7: Why use interfaces?
A: To define contracts and support multiple inheritance.

Q8: Can abstract classes have constructors?
A: Yes.

Q9: Can interfaces have fields?
A: No, only constants (static final).

Q10: Can interface methods have implementation?
A: Yes, default and static methods (Java 8+).

๐Ÿ“ MCQs

Q1. Can a class extend multiple abstract classes?

  • Yes
  • No
  • Sometimes
  • Only with interfaces

Q2. Can a class implement multiple interfaces?

  • No
  • Yes
  • Sometimes
  • Depends

Q3. What can abstract classes have?

  • Only methods
  • Fields, constructors, methods
  • Only constructors
  • Only fields

Q4. What can interfaces contain?

  • Fields
  • Method signatures and default/static methods
  • Constructors
  • Instance methods

Q5. Are interface methods public?

  • No
  • Yes
  • Sometimes
  • Never

Q6. Why use abstract classes?

  • To restrict inheritance
  • To share common code
  • For interfaces
  • For data hiding

Q7. Why use interfaces?

  • For implementation
  • To define contracts
  • To share code
  • For performance

Q8. Can abstract classes have constructors?

  • No
  • Yes
  • Sometimes
  • Never

Q9. Can interfaces have fields?

  • Yes
  • No, only constants
  • Sometimes
  • No

Q10. Can interface methods have implementation?

  • No
  • Yes
  • Sometimes
  • Only abstract methods

๐Ÿ’ก Bonus Insight

Choosing between abstract classes and interfaces depends on whether you want to share code or define capabilities; understanding this distinction is key in Java design.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup