Java Interface: Implement the Flyable interface

Java Interface: Exercise-3 with Solution

Write a Java program to create an interface Flyable with a method called fly_obj(). Create three classes Spacecraft, Airplane, and Helicopter that implement the Flyable interface. Implement the fly_obj() method for each of the three classes.

Sample Solution:

Java Code:

// Flyable.java
// Interface Flyable

interface Flyable {
    void fly_obj();

// Spacecraft.java
// Class Spacecraft
class Spacecraft implements Flyable {
    public void fly_obj() {
        System.out.println("Spacecraft is flying");
// Airplane.java
// Class Airplane 
class Airplane implements Flyable {
    public void fly_obj() {
        System.out.println("Airplane is flying");
// Helicopter.java
// Class Helicopter

class Helicopter implements Flyable {
    public void fly_obj() {
        System.out.println("Helicopter is flying");
// Main.java
// Class Main

public class Main {
    public static void main(String[] args) {
        Flyable[] flyingObjects = {new Spacecraft(), new Airplane(), new Helicopter()};

        for (Flyable obj : flyingObjects) {
            obj. fly_obj();

Sample Output:

Spacecraft is flying
Airplane is flying
Helicopter is flying

Flowchart of Interface Flyable:

Flowchart: Interface Flyable

Flowchart of Class Spacecraft:

Flowchart: Class Spacecraft

Flowchart of Class Airplane:

Flowchart: Class Airplane

Flowchart of Class Helicopter:

Flowchart: Class Helicopter

Flowchart of Class Main:

Flowchart: Class Main

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Shape with the getArea() method, implement the Shape interface.
Next: Banking system classes - Bank Account, Savings Account, and Current Account.

What is the difficulty level of this exercise?

Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How do I remove repeated elements from ArrayList?

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);

Of course, this destroys the ordering of the elements in the ArrayList.

Ref: https://bit.ly/3bYIjNC


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook