Write a Java program to Multiple Inheritance using Interfaces


This Java program demonstrates the use of interfaces, multiple interface implementation, and method overriding. In this program, there are two interfaces (Flyable and Swimmable) and a class (Bird) that implements both interfaces. Here's an explanation of the program:

  • public class Interfaces: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is where the program execution starts. It creates an object of the Bird class and demonstrates the use of methods defined in the Flyable and Swimmable interfaces.
  • Bird bird = new Bird();: This line creates an object of the Bird class.
  • bird.fly();: This line calls the fly method on the bird object, which is implemented from the Flyable interface. The fly method in the Bird class prints "Bird is Flying" to the console.
  • bird.swim();: This line calls the swim method on the bird object, which is implemented from the Swimmable interface. The swim method in the Bird class prints "Bird is Swimming" to the console.
  • interface Flyable: This is the first interface, which declares a single method, fly.
  • void fly();: The fly method in the Flyable interface is declared but not implemented. Any class that implements this interface is required to provide an implementation for this method.
  • interface Swimmable: This is the second interface, which declares a single method, swim.
  • void swim();: The swim method in the Swimmable interface is declared but not implemented. Like the fly method, any class that implements this interface is required to provide an implementation for this method.
  • class Bird implements Flyable, Swimmable: The Bird class implements both the Flyable and Swimmable interfaces, which means it must provide implementations for both the fly and swim methods.
  • @Override annotations: The @Override annotations are used to indicate that the fly and swim methods in the Bird class are intended to override methods from the implemented interfaces.
  • public void fly(): This is the implementation of the fly method in the Bird class, which prints "Bird is Flying" to the console.
  • public void swim(): This is the implementation of the swim method in the Bird class, which prints "Bird is Swimming" to the console.

This program demonstrates how to define and implement interfaces in Java. The Bird class implements both the Flyable and Swimmable interfaces, providing implementations for the fly and swim methods. This allows an object of the Bird class to exhibit behavior from both interfaces, effectively supporting both flying and swimming.


Source Code

public class Interfaces	 // Main class
{
	public static void main(String[] args)
	{
		Bird bird = new Bird();
		bird.fly();
		bird.swim();
	}
}
 
interface Flyable   // First interface
{
	void fly();
}
 
interface Swimmable	 // Second interface
{
	void swim();
}
 
class Bird implements Flyable, Swimmable  // Class implementing multiple interfaces
{
	@Override
	public void fly()
	{
		System.out.println("Bird is Flying");
	}
 
	@Override
	public void swim()
	{
		System.out.println("Bird is Swimming");
	}
}

Output

Bird is Flying
Bird is Swimming