0
0
Javaprogramming~30 mins

Abstract classes in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Abstract Classes in Java
πŸ“– Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle has a method to display its sound. You want to use an abstract class to define the common structure for all vehicles.
🎯 Goal: Build a Java program that uses an abstract class called Vehicle with an abstract method makeSound(). Then create two subclasses Car and Bike that provide their own implementation of makeSound(). Finally, create objects of these subclasses and call their makeSound() methods.
πŸ“‹ What You'll Learn
Create an abstract class called Vehicle with an abstract method makeSound().
Create a class Car that extends Vehicle and implements makeSound() to print "Car goes vroom".
Create a class Bike that extends Vehicle and implements makeSound() to print "Bike goes ring ring".
In the Main class, create one Car object and one Bike object.
Call the makeSound() method on both objects and print the results.
πŸ’‘ Why This Matters
🌍 Real World
Abstract classes help organize code when many objects share common features but have different specific behaviors, like different types of vehicles.
πŸ’Ό Career
Understanding abstract classes is important for designing flexible and reusable code in many software development jobs.
Progress0 / 4 steps
1
Create the abstract class Vehicle
Create an abstract class called Vehicle with an abstract method makeSound() that returns void.
Java
Need a hint?

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

2
Create the Car and Bike classes
Create a class called Car that extends Vehicle and implements the makeSound() method to print "Car goes vroom". Also create a class called Bike that extends Vehicle and implements the makeSound() method to print "Bike goes ring ring".
Java
Need a hint?

Remember to use extends Vehicle and override the makeSound() method with void makeSound().

3
Create objects of Car and Bike
Create a class called Main with a main method. Inside the main method, create one object of Car called myCar and one object of Bike called myBike.
Java
Need a hint?

Use new Car() and new Bike() to create the objects inside the main method.

4
Call makeSound() on the objects and print output
In the main method of the Main class, call the makeSound() method on the myCar and myBike objects to print their sounds.
Java
Need a hint?

Call myCar.makeSound() and myBike.makeSound() to print the sounds.