Interfaces for Multiple Inheritance in Java


Write a Java program to demonstrate the use of interfaces for achieving multiple inheritance

  • Two interfaces A and B are defined, each containing a single abstract method methodA() and methodB() respectively.
  • The class C implements both interfaces A and B.
  • In the C class, both methodA() and methodB() are overridden to provide concrete implementations.
  • In the Main class, an instance of class C is created.
  • The methodA() and methodB() are called on this instance, demonstrating that class C provides implementations for both methods from interfaces A and B.
  • When c.methodA() is called, it prints "Method A", and when c.methodB() is called, it prints "Method B".

Source Code

interface A
{
	void methodA();
}
 
interface B
{
	void methodB();
}
 
class C implements A, B
{
	@Override
	public void methodA()
	{
		System.out.println("Method A");
	}
 
	@Override
	public void methodB()
	{
		System.out.println("Method B");
	}
}
 
public class Main
{
	public static void main(String[] args)
	{
		C c = new C();
		c.methodA();
		c.methodB();
	}
}

Output

Method A
Method B

Example Programs