Extending Interfaces, Multiple Inheritance via Interfaces & Interface vs Abstract Class
default methods (with implementation bodies) and static helper methods; JDK 9 added private interface methods. When two interfaces define conflicting default methods, the implementing class MUST override the method to resolve ambiguity.1. Topic Introduction
Interfaces don't just live alone—an interface can inherit from one or multiple other interfaces using extends, and a single class can implement multiple interfaces simultaneously, cleanly giving Java safe multiple inheritance! Comparing Interfaces vs Abstract Classes is also one of the single most frequently tested comparison questions in KTU exams.
extends) & Carrying Multiple Licenses (implements A, B)Think of interface BasicDriver (can steer and brake) upgraded into interface ProDriver extends BasicDriver (adds highway & night driving). A class implementing ProDriver must obey ALL rules of both BasicDriver and ProDriver! Meanwhile, a person can simultaneously hold a Driving License, First-Aid Certificate, and Passport (class Paramedic implements Driver, Medic, Traveler)—no ambiguity because interfaces carry contracts, not conflicting parent instance state!
extends 1 class + implements N interfaces; Interfaces extends N interfaces (interface C extends A, B).3. Essential Exam Definitions
extends)interface B extends A1, A2).default Methoddefault keyword (default void log() { ... }), allowing libraries to add new interface methods without breaking existing implementing classes.4. Detailed Technical Explanation & Syntax
1. Keyword Combinations (extends vs implements)
Never mix up extends and implements in exam syntax:
- Class -> Class:
class Sub extends Super(single inheritance ONLY). - Class -> Interface(s):
class MyClass implements I1, I2(multiple allowed). - Class -> Class + Interfaces:
class Sub extends Super implements I1, I2(extendsMUST precedeimplements!). - Interface -> Interface(s):
interface SubIface extends I1, I2(interfacesextendsinterfaces—NEVERimplements!).
2. Multiple Inheritance Without Diamond Ambiguity
If a class implements two interfaces A and B that both declare void show();, the class provides ONE public void show() body satisfying both contracts cleanly. If A and B both provide default void show() { ... }, Java requires the implementing class to override show() (optionally calling A.super.show()), preventing silent diamond ambiguity.
5. Working Principle: Choosing Abstract Class vs Interface in Exam & Design
- Need shared instance fields (
balance,width), constructors, or non-public protected helpers across closely relatedIS-Aclasses? Choose Abstract Class. - Need a lightweight behavioral capability (
Comparable,Serializable,ActionListener) across unrelated classes or multiple inheritance? Choose Interface.
7. Complete Java Code Examples & Tracing
interface B extends A, multiple interface implementation (Printable, Scannable), and JDK 8 default conflict resolution.| 1 | interface BasicDevice { |
| 2 | void powerOn(); |
| 3 | } |
| 4 | |
| 5 | // Interface extending another interface |
| 6 | interface SmartDevice extends BasicDevice { |
| 7 | void connectWifi(String ssid); |
| 8 | } |
| 9 | |
| 10 | interface Printable { |
| 11 | default void status() { |
| 12 | System.out.println("Printable status: Ready"); |
| 13 | } |
| 14 | void printDoc(String doc); |
| 15 | } |
| 16 | |
| 17 | interface Scannable { |
| 18 | default void status() { |
| 19 | System.out.println("Scannable status: Calibrated"); |
| 20 | } |
| 21 | void scanDoc(); |
| 22 | } |
| 23 | |
| 24 | // Multiple interface inheritance + extended interface |
| 25 | public class MultipleInterfaceDemo implements SmartDevice, Printable, Scannable { |
| 26 | @Override |
| 27 | public void powerOn() { |
| 28 | System.out.println("MultiFunction Printer powered ON."); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public void connectWifi(String ssid) { |
| 33 | System.out.println("Connected to Wi-Fi: " + ssid); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void printDoc(String doc) { |
| 38 | System.out.println("Printing: " + doc); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void scanDoc() { |
| 43 | System.out.println("Scanning document..."); |
| 44 | } |
| 45 | |
| 46 | // Must resolve conflicting default methods from Printable & Scannable |
| 47 | @Override |
| 48 | public void status() { |
| 49 | Printable.super.status(); |
| 50 | Scannable.super.status(); |
| 51 | } |
| 52 | |
| 53 | public static void main(String[] args) { |
| 54 | MultipleInterfaceDemo mfp = new MultipleInterfaceDemo(); |
| 55 | mfp.powerOn(); |
| 56 | mfp.connectWifi("KTU-Lab"); |
| 57 | mfp.status(); |
| 58 | mfp.printDoc("OOP_Notes.pdf"); |
| 59 | mfp.scanDoc(); |
| 60 | } |
| 61 | } |
MultiFunction Printer powered ON. Connected to Wi-Fi: KTU-Lab Printable status: Ready Scannable status: Calibrated Printing: OOP_Notes.pdf Scanning document...
interfaceextendsimplementsdefaultsuper@Override- Writing
interface B implements Ainstead ofinterface B extends A. - Writing
class C implements I1 extends Base(wrong order—extendsmust come beforeimplements!).
Comparison Tables
| Comparison Basis | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract class | interface |
| Inheritance Keyword | Subclass uses extends | Class uses implements; Interface uses extends |
| Multiple Inheritance | No (class extends at most 1 class) | Yes (class implements multiple interfaces) |
| Methods | Can have both abstract and concrete methods | Abstract by default; JDK 8+ supports default/static, JDK 9+ private |
| Variables / Fields | Can have private, protected, static, non-final instance vars | All fields are implicitly public static final constants |
| Constructors | Can have constructors (called via super()) | Cannot have constructors |
| Access Modifiers | Members can be private, protected, default, public | Methods/fields implicitly public (except JDK 9 private helpers) |
| Use Case | Closely related IS-A hierarchy sharing state/code | Unrelated classes sharing CAN-DO contracts |
Real-World Applications
- Multiple interface implementation powers Java classes that are simultaneously
Comparable,Serializable, andCloneable.
Important KTU Exam Points
- Memorize at least 6 rows of the Abstract Class vs Interface comparison table.
- Always include Java version nuance: pre-JDK 8 (pure abstract methods) vs JDK 8 (
default&staticmethods).
Memory Techniques & Revision
- Syntax Rule:
Class extends Class,Class implements Interface(s),Interface extends Interface(s)!
Common Pitfalls to Avoid
- Claiming 'Interfaces can NEVER contain method bodies' without noting JDK 8
default/staticmethods.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: Both abstract classes and interfaces achieve abstraction in Java and cannot be instantiated directly, yet they differ fundamentally in inheritance, state, and method design.
- 1. Multiple Inheritance: A class can extend only one abstract class, whereas it can implement multiple interfaces.
- 2. Fields: Abstract classes can declare mutable instance variables (
private/protected); interface fields are implicitlypublic static finalconstants. - 3. Constructors: Abstract classes may define constructors; interfaces cannot have constructors.
- 4. Methods: Abstract classes contain concrete and abstract methods freely; interfaces contain abstract contracts (plus JDK 8
default/staticmethods). - 5. Design Fit: Use abstract classes for
IS-Afamilies sharing common code/state; use interfaces for capabilities across unrelated classes.
Quick Understanding Check
1. Which class declaration is syntactically VALID in Java?