Multiple interfaces in Java

Learn about Multiple interfaces in java with code example i.e. how to write more that one interfaces and how a class can implement them.

Multiple interface implementation is easy. We’ll learn it by a code example. but, we should be familiar with following concept.

Now, lets understand multiple interface in java by example.

In below program example, there are two interfaces Flyable and Eatable. Flyable interface contains a method fly() and Eatable interface contains a method eat(). Both the methods are unimplemented.

There is a class called “Bird”, that will implement both interfaces with syntax given below .

class Bird implements Flyable, Eatable

Then, the bird class will override and implement methods fly and eat from both interfaces. See below, the complete code example, how the bird class is implementing multiple interface in java.

Example of multiple interfaces Implementation in Java

/*
 * Implementation of multiple interfaces java example
 */

interface Flyable {
	void fly();
}

interface Eatable {
	void eat();
}

// Bird class will implement both interfaces
class Bird implements Flyable, Eatable {

	public void fly() {
		System.out.println("Bird flying");
	}

	public void eat() {
		System.out.println("Bird eats");
	}
	// It can have more own methods.
}

/*
 * Test multiple interfaces example
 */
public class Sample {

	public static void main(String[] args) {

		Bird b = new Bird();
		b.eat();
		b.fly();
	}

}

Output:
Bird eats
Bird flying

Related Posts