Orientação a Objetos - Reescrita de Métodos em Java - Aula 20
Understanding Method Overriding in Object-Oriented Programming
Introduction to Method Overriding
- The speaker introduces the topic of method overriding within object-oriented programming, specifically focusing on how it allows for customization of inherited methods.
- The example used is a vehicle class where the default behavior (e.g., "vehicle is braking") can be overridden in a subclass (e.g., "car is braking").
Implementing Method Overriding
- The speaker emphasizes the importance of understanding inheritance before diving into method overriding, suggesting viewers review previous lessons on inheritance.
- A demonstration begins with creating an overridden method for "vehicle moving," showcasing how to specify actions unique to the car class.
Practical Example of Overriding
- The code snippet illustrates using
superto call the parent class's method while adding specific functionality for the child class.
- An example output shows that when invoking the overridden method, it correctly displays "the car is moving" instead of the generic vehicle message.
Further Customization and Testing
- The speaker continues by implementing another overridden method for braking, demonstrating how each subclass can have its own distinct messages.
- Upon testing, it confirms that when calling methods from different objects (like cars and motorcycles), they produce tailored outputs reflecting their specific behaviors.
Conclusion and Next Steps
- The session wraps up by highlighting how overriding enhances clarity and specificity in code, especially when dealing with multiple object types.