What is the reason for creating an interface in Java inheritance?

Asked 2 years ago, Updated 2 years ago, 25 views

It's been a month and a half since I learned Java. What is the reason for creating an interface in Java inheritance?

java

2022-09-22 20:04

1 Answers

You can detach the function and it's a documentation about the function.

interface car {
    void drive();
}

interface shovel {
    void dig();
}

// The forklift is a car with a shovel. It has two functions.
class forklane implementations car, shovel {
    public void drive() {
        // Automotive Implementation for Forklane
    }
    public void dig(){
        // Implementation of a forklift shovel
    }
}

class bus implementations car {
    public void drive(){
        // Implementation of a bus car
    }
}

// You can drive your cars through the driver class execute as shown below.
class driver {
    public void execute {
        car.drive();
    }
}

// This can be done as follows:
automobile forkcrane = new forklift();
car bus = new bus();

driver = new driver ();
driver.execute(forkcrane); // forklift operation
driver.execute(bus); // bus driving


2022-09-22 20:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.