Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Declare a class Vehicle. From this class derive two_wheeler, three_wheeler and four_wheeler class. Display properties of each type of vehicle using member function of the class.

Short Answer

Expert verified
Define a base class `Vehicle` and inherit `TwoWheeler`, `ThreeWheeler`, and `FourWheeler` classes from it. Implement and use display functions to show properties.

Step by step solution

01

Define the Base Class

Start by defining the `Vehicle` class. This class will act as the base class from which other classes will inherit. In Python, this can be done using the `class` keyword followed by the class name. The `Vehicle` class can have properties like `make`, `model`, `year`, and `type`. It should have a method to display these properties.
02

Define the Derived Classes

Define the derived classes: `TwoWheeler`, `ThreeWheeler`, and `FourWheeler`. Each of these classes will inherit from the `Vehicle` class. Use the syntax `class DerivedClass(BaseClass):` for inheritance. Each derived class can have additional methods or properties specific to that type of vehicle if needed.
03

Implement Member Functions

For each derived class, implement a member function that displays its properties. This function can override the `display_properties` method from the `Vehicle` class if additional properties are added or customization is needed for specific vehicle types.
04

Create Instances and Display Properties

Create instances of each derived class, populate them with specific data, and call the `display_properties` method. This will demonstrate polymorphism as each class will utilize its own version of the `display_properties` function, showing how the same method end behaves differently depending on the object's actual class.
05

Code Implementation

```python class Vehicle: def __init__(self, make, model, year, type): self.make = make self.model = model self.year = year self.type = type def display_properties(self): print(f"Make: {self.make}, Model: {self.model}, Year: {self.year}, Type: {self.type}") class TwoWheeler(Vehicle): pass # Inherits everything from Vehicle class ThreeWheeler(Vehicle): pass # Inherits everything from Vehicle class FourWheeler(Vehicle): pass # Inherits everything from Vehicle # Create instances and display properties bike = TwoWheeler('Yamaha', 'YZF-R3', 2020, 'Motorbike') bike.display_properties() rickshaw = ThreeWheeler('TVS', 'King', 2019, 'Auto Rickshaw') rickshaw.display_properties() car = FourWheeler('Toyota', 'Corolla', 2021, 'Car') car.display_properties() ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Inheritance in Programming
Inheritance is a core concept in object-oriented programming that allows a new class to receive or inherit the properties and behaviors of an existing class. This powerful feature enables code reusability and a logical hierarchy.

When we define a new class in programming, it can be beneficial to reuse functionality from a previously defined class. The new class, known as the derived class, inherits methods and properties from a base class. This way, developers avoid redundancy and write cleaner, more modular code.
  • The base class, sometimes called the parent class, holds common properties that can be shared by multiple derived classes.
  • Derived classes, also known as child classes, can extend or modify the behavior of the base class.
  • Though a derived class inherits from a base class, it can also add new functionality or override existing methods.
In our exercise, the `Vehicle` class serves as the base class that holds properties like make, model, year, and type. `TwoWheeler`, `ThreeWheeler`, and `FourWheeler` extend `Vehicle`, inheriting its properties. This is a simple yet effective way to represent different types of vehicles.
Class and Object Definition
In object-oriented programming, defining classes and creating objects are fundamental operations. A class is a blueprint for objects, representing a group of related attributes and methods. An object, on the other hand, is an instance of a class with specific values for its properties.
  • A class definition begins with the `class` keyword followed by an identifier.
  • Classes encapsulate data for the object, defining fields for properties and methods to operate on those fields.
  • Objects are instances of a class created using the class constructor, which initializes the object's properties.
In our exercise, the `Vehicle` class is defined with properties like `make`, `model`, `year`, and `type`. Each property describes an attribute of a vehicle, and the class may also contain methods like `display_properties` to show these details.
To create an object of `Vehicle`, we call the constructor, passing the necessary initial property values. For example, `TwoWheeler('Yamaha', 'YZF-R3', 2020, 'Motorbike')` creates a new bike object. Here, the bike object has a specific make, model, year, and type, based on the values passed during its creation.
Polymorphism in Java
Polymorphism is another foundational principle of object-oriented programming, allowing methods to perform in different ways based on the object they are called on. It typically manifests through method overriding and enables dynamic method dispatch.
  • Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class.
  • Dynamic method dispatch means that the method implementation is determined at runtime based on the object class, not the reference type.
Polymorphism enables code to be more flexible and adaptive. For instance, even if different vehicle types override the `display_properties` method, when called, the correct version of the method is executed relevant to the specific object.

In our exercise, the `display_properties` method in the `Vehicle` class can be overridden by any derived class like `TwoWheeler` or `FourWheeler` to include additional details specific to those types. Java's runtime environment handles which method to invoke based on the object type, demonstrating polymorphism. Though our exercise uses Python, similar principles apply to Java and other object-oriented languages.
Displaying Object Properties
Displaying object properties involves retrieving and showing the data held within an object. When programming, it's common to define methods to format and present these attributes clearly and understandably.
  • The method `display_properties` is a common pattern used to output an object's attributes.
  • This method typically accesses the object's properties and prints them in a readable format.
In our example, the `Vehicle` class contains a `display_properties` method. It prints out the `make`, `model`, `year`, and `type` of the vehicle. This basic functionality is crucial in any object-oriented system to verify and demonstrate the information stored within objects.
When `display_properties` is invoked on a derived class instance, any additional properties or customized output will appear, depending on how and if the method was overridden. This flexibility showcases the usefulness of having a dedicated method for property presentation and can be adapted easily for any additional attributes as needed.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free