✏️ Explanatory Question

[Inheritance]

Based on the given image, write the Java statement to create the class Child.
Inheritance

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

The correct Java statement is:
class Child implements Father, Mother { }

Understanding the Diagram:

The image shows:

  • Two interfaces: Father and Mother
  • One class: Child
  • The class Child is connected to both interfaces

This represents:

Multiple Inheritance using Interfaces

Important Concept:

In Java:

  • A class cannot extend multiple classes
  • But a class CAN implement multiple interfaces

Syntax for Implementing Multiple Interfaces:

class ClassName implements Interface1, Interface2 { }

Applying to the Given Diagram:

Component Type
Father Interface
Mother Interface
Child Class

Therefore, the class declaration becomes:

class Child implements Father, Mother { }

Why implements is Used?

The keyword:

implements

is used when a class inherits behavior from interfaces.

Why extends is Not Used?

extends is used for:

  • Class-to-class inheritance
  • Interface-to-interface inheritance

But here:

  • Child is a class
  • Father and Mother are interfaces

So Java requires:

implements

Example with Method Implementation:

interface Father { void showFather(); } interface Mother { void showMother(); } class Child implements Father, Mother { public void showFather() { System.out.println("Father Method"); } public void showMother() { System.out.println("Mother Method"); } }

Final Conclusion:

  • The diagram represents multiple inheritance using interfaces.
  • A class implements interfaces using the keyword implements.
  • Therefore, the correct statement is:
class Child implements Father, Mother { }