20. Protected Inheritance

Your task is to define two classes:

  • Base class: Device
    • Has a public method void showType() that prints:
      Generic Device
       
  • Derived class: Sensor (inherits protectedly from Device)
    • Has a public method void showSensor() that:
      1. Calls showType() (from base, but accessible inside derived).
      2. Prints:
        Sensor Device


The program will:

  1. Create a Sensor object.
  2. Call only showSensor() — not showType() directly.
     

Example

Output:

Generic Device
Sensor Device

 

Why this output?

Because showType() is inherited as protected, it can be called from inside Sensor, but not from main().

 

Question Significance

Shows how protected inheritance hides base methods from outside while still allowing derived classes to use them.

Loading...

Input

Expected Output

Generic Device Sensor Device