In Java, what is the difference between an interface and an abstract class in terms of inheritance, method implementation, and typical usage?

Difficulty: Easy

Correct Answer: An interface defines a contract of methods that a class must implement and supports multiple inheritance of types, while an abstract class can provide partial implementation and allows a single superclass with shared state and behavior.

Explanation:


Introduction / Context:
Interfaces and abstract classes are central concepts in object oriented design with Java. Both are used to define abstractions, but they serve different roles and have different capabilities. This question checks whether you understand how they differ in inheritance rules, method implementations, and when to choose one over the other.


Given Data / Assumptions:

    • Java supports single inheritance of classes but allows a class to implement multiple interfaces.
    • An abstract class can contain both implemented and abstract methods.
    • An interface traditionally contains abstract methods, and in modern Java it can also contain default and static methods.
    • Classes that use these constructs must follow Java inheritance rules.


Concept / Approach:
An interface defines a pure contract: it specifies methods that a class promises to provide. Before Java 8, interface methods were implicitly public and abstract, and fields were public static final. A class could implement many interfaces, which allows a form of multiple inheritance of types. An abstract class, by contrast, is a class that cannot be instantiated but can contain fields, constructors, concrete methods, and abstract methods. A class can extend only one superclass, abstract or concrete, and inherits its implementation and state, which makes abstract classes ideal for sharing common code and data.


Step-by-Step Solution:
Step 1: Note that an interface is implemented using the implements keyword, while an abstract class is extended with the extends keyword.Step 2: A class can implement multiple interfaces but can extend only one class, so interfaces support multiple type inheritance and abstract classes support single inheritance of implementation.Step 3: Abstract classes can declare instance variables, define constructors, and provide protected methods that share code among subclasses.Step 4: Interfaces are best used to express capabilities such as Comparable or Runnable, where unrelated classes can implement the same contract.Step 5: Option A correctly summarizes these differences in contract versus partial implementation and multiple versus single inheritance.


Verification / Alternative check:
To verify, try to create a Java class that extends two abstract classes; the compiler reports an error because multiple class inheritance is not allowed. However, the same class can implement several interfaces without issue. Similarly, you can add fields and fully implemented methods to an abstract class, while interface fields are implicitly constants and interface methods without default bodies are abstract contracts that subclasses must override.


Why Other Options Are Wrong:
Option B reverses the roles of abstract classes and interfaces, giving interfaces state and implementation and taking them away from abstract classes. Option C claims you can instantiate an interface, which is not allowed, and says abstract classes cannot be subclassed, which is incorrect. Option D ignores real language differences and is therefore wrong.


Common Pitfalls:
A common mistake is using an abstract class where an interface would be more flexible, or vice versa. Another pitfall is overusing interfaces for simple hierarchies where an abstract base class that shares code would reduce duplication. In modern Java, the addition of default methods in interfaces makes design choices more subtle, but the key idea remains that interfaces describe capabilities and abstract classes share common implementation and state.


Final Answer:
An interface defines a contract of methods that a class must implement and supports multiple inheritance of types, while an abstract class can provide partial implementation and allows a single superclass with shared state and behavior.

More Questions from Technology

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion