Abstract Classes vs Interfaces, Default Methods & Marker Interfaces
Designing polymorphic contracts, multiple inheritance of type, and tagging interfaces.
Coding problems with instant test-case feedback
Write Python, C++, C or JavaScript in your browser and see which hidden tests pass.
Designing clean software architectures requires knowing when to use an Abstract Class and when to use an Interface.
1. Characteristics of an Abstract Class#
From PDF Page 8, Question 22: The 5 core characteristics of an abstract class are:
1. Instantiation of an abstract class is not allowed. It must be inherited.
2. An abstract class can have both abstract and non-abstract (concrete) methods.
3. An abstract class must declare at least one abstract method.
4. It is always public in interface contracts.
5. Can have instance variables, static variables, and constructors.
2. Abstract Class vs Interface Face-Off#
Tricky Question 12: Comparing the two core abstraction tools:
| Feature | Interface | Abstract Class |
|---|---|---|
| Multiple Inheritance | Supports multiple inheritance of type (implements A, B) | Single inheritance only (extends A) |
| Method Implementations | Primarily abstract; can have default and static methods | Can contain both abstract and fully implemented concrete methods |
| State & Fields | Cannot store state; only public static final constants | Can maintain mutable instance variables and constructors |
| Speed | Slightly slower due to search in interface table | Fast direct vtable resolution |
Practise placement aptitude on the real test pattern
TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.
3. Default Methods in Interfaces & Marker Interfaces#
• Default Method (Tricky Question 27): Introduced in Java 8 to allow adding new methods to interfaces without breaking existing implementing classes.
• Marker Interface (Tricky Question 21): An empty interface containing no methods or fields (e.g., `java.io.Serializable`, `Cloneable`). It marks a class for special runtime or JVM handling.
Test Your Concept Mastery
Verify your retention on: Abstract Classes vs Interfaces, Default Methods & Marker Interfaces
What is a marker interface in Java?
Ready for a full proctored assessment?
Take a free aptitude mock on a real company pattern, with a worked solution for every question.