Example 1: Example to show Multiple Inheritance using interface.

import java.io.*;

interface A
  {  
   void display();  
  }  
class B implements A
  {  
     public void display()
     {
      System.out.println("Welcome India");
     }  
  }  
class C implements A
   {  
      public void display()
      {
        System.out.println("Welcome World");
      }  
   }  
class D
   {  
      public static void main(String args[])
      {  
        A a=new C();  
        a.display();

        A b=new B();
        b.display();  
      }
   } 

Save as - A.java
Compile as - javac A.java
Interpreted/Run as - java D 

Output:
Welcome World
Welcome India

Example 2: Example to implement/use Multiple Interface in a java class.

import java.io.*;

interface A
  {  
     void display();  
  }  
interface B
  {  
     void output();  
  }  
class C implements A,B
 {  
     public void display ()
      {
	System.out.println("Welcome India");
      }  
     public void output()
      {
	System.out.println("Welcome World");
      }    
     public static void main(String args[])
      {  
	C c1 = new C();  
	c1. display ();  
        c1. output ();  
      }
  } 

Output:
Welcome India
Welcome World

---------  OR  ----------
import java.io.*;
interface A
  {    
    final int a=20;
    void display();
  }
interface B
  {
    void display();
  }
class C implements A,B
  {
        public void display()
        { 
	   System.out.println("Codershelpline to all");	
	} 
	public static void main(String args[])
    	{
	    C x=new C();
	    x.display();
	    System.out.println(a);      
    	}
   }
Output:
Codershelpline to all.
20

Example 3: Example to show interface Inheritance.

import java.io.*;
interface A
 {
   public void display();
 }
interface B extends A
 {
   public void output();
 }
class C implements B
 {
    public void display()
    {
      System.out.println("Welcome India");
    }
    public void output()
    {
      System.out.println("Welcome World");
    }
    public static void main(String args[])
    {
      A a=new C();   //arise error due to no implementation directly.
      B a = new C();
      a.output();
      a.display();
    }
 }
Output: 
Welcome World
Welcome India

Example 4: Interface implementation fails when methods with same name but different return type.

import java.io.*;

interface A
{
   public void display();
}
interface B
{
   public int display();
}
class C implements A,B
{
   public void display() 
   {
   }
   public int display() 
   {
   }
   public static void main(String args[])
   {
       A a = new C();
       a.display();
   }
}
Output: 
return type void is not compatible with int

Example 5: Interface implementation with similar Variable names.

import java.io.*;
interface A
{
    int a=50;
}
interface B
{
    int a=500;
}
class C implements A,B
{
    public static void main(String args[])
    {       
       //System.out.println(a);  generates ambiguity error
       System.out.println(A.a);
       System.out.println(B.a);
    }
}
Output: 
50
500

Example 6: Example to show Interface using static method.

import java.io.*;
interface A
 {
     static void display()
     {
        System.out.println("Codershelpline to all");
     }
 }
class B implements A
 {
     public static void main(String args[])
     {
	A.display();       
     }
 }
Output: 
Codershelpline to all

Example 7: Example to show Interface using default method.

import java.io.*;
interface A
{
    default void display()
     {
	System.out.println("Codershelpline to all");
     }
}
class B implements A
{
     public static void main(String args[])
    	{
	    B x=new B();
	    x.display();       
    	}
}
Output: 
Codershelpline to all

 439 total views,  1 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.