KTU OOP Java2024 Scheme • 60M ESE
MODULE 3/Section 2: Interfaces

Extending Interfaces, Multiple Inheritance via Interfaces & Interface vs Abstract Class

HIGH PRIORITY (Prep Guide)Official Syllabus~15 min study
Official KTU 2024 Syllabus Topics Covered Here
Extending interfacesMultiple interface inheritanceInterfaces versus abstract classesComplete Java examples
Java Version Note: Since JDK 8, interfaces can include 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.

2. Start From Zero: Upgrading a Permit (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!

Key Takeaway: Classes extends 1 class + implements N interfaces; Interfaces extends N interfaces (interface C extends A, B).

3. Essential Exam Definitions

Interface Inheritance (extends)
Mechanism where one interface inherits constants and method declarations from one or more parent interfaces (interface B extends A1, A2).
JDK 8 default Method
A concrete method defined inside an interface using the default 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 (extends MUST precede implements!).
  • Interface -> Interface(s): interface SubIface extends I1, I2 (interfaces extends interfaces—NEVER implements!).

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

  1. Need shared instance fields (balance, width), constructors, or non-public protected helpers across closely related IS-A classes? Choose Abstract Class.
  2. Need a lightweight behavioral capability (Comparable, Serializable, ActionListener) across unrelated classes or multiple inheritance? Choose Interface.

7. Complete Java Code Examples & Tracing

Extending Interfaces + Multiple Interface Inheritance + JDK 8 Default MethodMultipleInterfaceDemo.java
Demonstrates interface B extends A, multiple interface implementation (Printable, Scannable), and JDK 8 default conflict resolution.
1interface BasicDevice {
2 void powerOn();
3}
4
5// Interface extending another interface
6interface SmartDevice extends BasicDevice {
7 void connectWifi(String ssid);
8}
9
10interface Printable {
11 default void status() {
12 System.out.println("Printable status: Ready");
13 }
14 void printDoc(String doc);
15}
16
17interface Scannable {
18 default void status() {
19 System.out.println("Scannable status: Calibrated");
20 }
21 void scanDoc();
22}
23
24// Multiple interface inheritance + extended interface
25public 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}
Expected Output
MultiFunction Printer powered ON.
Connected to Wi-Fi: KTU-Lab
Printable status: Ready
Scannable status: Calibrated
Printing: OOP_Notes.pdf
Scanning document...
Keywords:interfaceextendsimplementsdefaultsuper@Override
Common Exam & Coding Mistakes
  • Writing interface B implements A instead of interface B extends A.
  • Writing class C implements I1 extends Base (wrong order—extends must come before implements!).

Comparison Tables

Interfaces vs Abstract Classes (High-Yield KTU 6/9-Mark Comparison!)
Comparison BasisAbstract ClassInterface
Keywordabstract classinterface
Inheritance KeywordSubclass uses extendsClass uses implements; Interface uses extends
Multiple InheritanceNo (class extends at most 1 class)Yes (class implements multiple interfaces)
MethodsCan have both abstract and concrete methodsAbstract by default; JDK 8+ supports default/static, JDK 9+ private
Variables / FieldsCan have private, protected, static, non-final instance varsAll fields are implicitly public static final constants
ConstructorsCan have constructors (called via super())Cannot have constructors
Access ModifiersMembers can be private, protected, default, publicMethods/fields implicitly public (except JDK 9 private helpers)
Use CaseClosely related IS-A hierarchy sharing state/codeUnrelated classes sharing CAN-DO contracts

Real-World Applications

  • Multiple interface implementation powers Java classes that are simultaneously Comparable, Serializable, and Cloneable.

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 & static methods).

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/static methods.

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 implicitly public static final constants.
  • 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/static methods).
  • 5. Design Fit: Use abstract classes for IS-A families sharing common code/state; use interfaces for capabilities across unrelated classes.

Quick Understanding Check

1. Which class declaration is syntactically VALID in Java?