Chapter 8: Problem 18
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.
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.
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.
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.
- 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.
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.
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.
- 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.
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.
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.
- 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.
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.