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


Returns all the distinct values of an array.

Uses Arrays.stream().distinct() to discard all duplicated values.

public static int[] distinctValuesOfArray(int[] elements) {
    return Arrays.stream(elements).distinct().toArray();

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


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