0
0
Javaprogramming~30 mins

Abstract methods in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract Methods in Java
πŸ“– Scenario: Imagine you are creating a simple program for different types of vehicles. Each vehicle can start its engine, but the way it starts is different for each type.
🎯 Goal: You will create an abstract class with an abstract method, then create subclasses that provide their own version of this method. Finally, you will create objects and call the method to see the different outputs.
πŸ“‹ What You'll Learn
Create an abstract class called Vehicle with an abstract method startEngine().
Create two subclasses called Car and Motorcycle that extend Vehicle.
Override the startEngine() method in both subclasses with their own messages.
Create objects of Car and Motorcycle and call their startEngine() methods.
πŸ’‘ Why This Matters
🌍 Real World
Abstract classes and methods help organize code when different objects share common behavior but implement details differently, like vehicles starting engines.
πŸ’Ό Career
Understanding abstract methods is important for designing flexible and reusable code in many software development jobs.
Progress0 / 4 steps
1
Create the abstract class
Create an abstract class called Vehicle with an abstract method startEngine() that returns void.
Java
Need a hint?

Use the abstract keyword before the class and method. The method has no body.

2
Create subclasses with method overrides
Create two classes called Car and Motorcycle that extend Vehicle. Override the startEngine() method in both classes to print "Car engine started" and "Motorcycle engine started" respectively.
Java
Need a hint?

Use extends Vehicle and add @Override before the method.

3
Create objects and call methods
In a Main class, create a main method. Inside it, create one object of Car called myCar and one object of Motorcycle called myMotorcycle. Call startEngine() on both objects.
Java
Need a hint?

Create objects with new and call the method with dot notation.

4
Run and display output
Run the program and print the output of calling startEngine() on both myCar and myMotorcycle.
Java
Need a hint?

When you run the program, you should see two lines printed, one for each vehicle.