878195. Abstraction in Java
Easy
Abstraction in Java
Slug: abstraction-in-java
Difficulty: Easy
Id: 878195
Topic Tags: None
Company Tags: None
Summary
Abstraction is a fundamental concept in object-oriented programming. In this problem, we'll explore how to implement abstraction in Java using abstract classes and interfaces. We'll learn how to define an interface or abstract class that provides a blueprint for objects to follow without specifying exactly how they should be implemented.
Detailed Explanation
Let's consider a real-world example where we have different types of vehicles like Car, Bus, Truck, etc. Each vehicle has its own characteristics like speed, color, number of wheels, etc. We can create an abstract class called Vehicle that provides the common attributes and methods for all vehicles.
public class Main {
public static void main(String[] args) {
Vehicle car = new Car("Red", 60);
car.displayDetails();
Vehicle bus = new Bus("Blue", 40);
bus.displayDetails();
Vehicle truck = new Truck("Green", 30);
truck.displayDetails();
}
}