19. Vehicle Inheritance Public

Your task is to define two classes:

  • Base class: Vehicle
    • A method void printCategory() that prints:
      Generic Vehicle

       

  • Derived class: Car (inherits publicly from Vehicle)
    • A method void printCar() that prints:
      Car Vehicle


The program will:

  1. Create a Car object.
  2. Call printCategory() (inherited from base).
  3. Call printCar() (defined in derived).

     

Example

Output:

Generic Vehicle
Car Vehicle

 

Why this output?

  • The base class method printCategory() is accessible through the derived class because of public inheritance.
  • The derived class adds its own method printCar() and prints its message.
     

Question Significance

This reinforces the idea of reusing base functionality and extending it in derived classes.

Loading...

Input

Expected Output

Generic Vehicle Car Vehicle