Hybrid Abstract + Interface Example
This program demonstrates hybrid modeling using abstract classes and interfaces.
Problem Statement
Write a Java program where abstract class Vehicle and interface Fuel are combined in Car.
Source Code
| 1 | abstract class Vehicle { abstract void drive(); } |
| 2 | interface Fuel { void refuel(); } |
| 3 | class Car extends Vehicle implements Fuel { |
| 4 | public void drive(){ System.out.println("Eduinq Car Driving"); } |
| 5 | public void refuel(){ System.out.println("Eduinq Car Refueled"); } |
| 6 | } |
| 7 | public class AbstractInterfaceHybrid { |
| 8 | public static void main(String[] args) { |
| 9 | System.out.println("Eduinq Abstract + Interface Hybrid"); |
| 10 | Car c=new Car(); |
| 11 | c.drive(); c.refuel(); |
| 12 | } |
| 13 | } |
Program Output
Eduinq Abstract + Interface Hybrid Eduinq Car Driving Eduinq Car Refueled
Explanation
Abstract classes provide partial implementation and enforce structure. - Interfaces define contracts with no implementation. - Combining both allows flexible design: Car inherits driving behavior structure and implements refueling contract. - This hybrid approach is common in enterprise systems where some behaviors are shared while others must remain flexible and interchangeable. Conclusion: Shows abstract vs interface hybrid modeling.