Overriding methods with override
📖 Scenario: Imagine you are creating a simple app that manages different types of vehicles. Each vehicle can describe itself with a message. You will learn how to change this message for specific vehicle types by overriding a method.
🎯 Goal: Build a Kotlin program that defines a base class Vehicle with a method describe(). Then create a subclass Car that overrides the describe() method to provide a custom message. Finally, print the description from both classes.
📋 What You'll Learn
Create a base class called
Vehicle with a method describe() that returns the string "This is a vehicle."Create a subclass called
Car that inherits from VehicleOverride the
describe() method in Car to return the string "This is a car." using the override keywordCreate instances of
Vehicle and Car and print the result of calling describe() on each💡 Why This Matters
🌍 Real World
Overriding methods is common when you want different types of objects to behave differently while sharing a common interface, like different vehicles describing themselves uniquely.
💼 Career
Understanding method overriding is essential for Kotlin developers working on apps that use inheritance and polymorphism to organize code cleanly and reuse functionality.
Progress0 / 4 steps