Example : 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 : How 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 obj = new C();  
	obj.display ();  
        obj.output ();  
      }
  } 

Output:
Welcome India
Welcome World

---------  OR  ----------

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

	    System.out.println(a);      
    	}
   }

Output:
Codershelpline.
20

---------  OR  ----------

import java.io.*;
interface A
  {    
    final int a=30;    
  }
interface B
  {
    void display();
  }
class C implements A,B
  {
        public void display()
        { 
	    System.out.println("Hello India");
            System.out.println(a);  	
	} 
	public static void main(String args[])
    	{
	    C x=new C();
	    x.display();	        
    	}
   }

Output:
Hello India
30

Example : 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 : A Java program to implement Interface (fails) when methods with the same name but different return types.

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 : 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 : Example to show Interface using a 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 : Example to show Interface using the default method.

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

Output: 
Hello India

Example : A Java program to show/use runnable Interface in a Java program.

class A implements Runnable 
{
   public void run() 
   {
       // Code to be executed in a separate t1 and t2 thread
       for (int i = 0; i < 5; i++)
       {
           System.out.println("Thread Id "+Thread.currentThread().getId() + " executed with " + i + " loop.");
       }
   }
   public static void main(String[] args) 
   {
       A obj = new A();
       
       // Create a Thread object and pass the instance of A
       Thread t1 = new Thread(obj);
       Thread t2 = new Thread(obj);
       
       // Start the t1 and t2 thread
       t1.start();
       t2.start();
   }
}

Output:

Thread Id 12 executed with 0 loop.
Thread Id 11 executed with 0 loop.
Thread Id 12 executed with 1 loop.
Thread Id 11 executed with 1 loop.
Thread Id 12 executed with 2 loop.
Thread Id 11 executed with 2 loop.
Thread Id 12 executed with 3 loop.
Thread Id 12 executed with 4 loop.
Thread Id 11 executed with 3 loop.
Thread Id 11 executed with 4 loop.

-----------------------  OR  -------------------------

class A implements Runnable 
{
   public void run() 
   {
       // Code to be executed in a separate t1 and t2 thread
       for (int i = 0; i < 5; i++)
       {
           System.out.println("Thread Id "+Thread.currentThread().getId() + " executed with " + i + " loop.");
       }
   }
}

class B
{
   public static void main(String[] args) 
   {
       A obj = new A();
       
       // Create a Thread object and pass the instance of A
       Thread t1 = new Thread(obj);
       Thread t2 = new Thread(obj);
       
       // Start the t1 and t2 thread
       t1.start();
       t2.start();
   }
}

Output:
Thread Id 11 executed with 0 loop.
Thread Id 12 executed with 0 loop.
Thread Id 11 executed with 1 loop.
Thread Id 11 executed with 2 loop.
Thread Id 11 executed with 3 loop.
Thread Id 11 executed with 4 loop.
Thread Id 12 executed with 1 loop.
Thread Id 12 executed with 2 loop.
Thread Id 12 executed with 3 loop.
Thread Id 12 executed with 4 loop.

-----------------------  OR  -------------------------

class A implements Runnable 
{
   public void run() 
   {
       // Code to be executed in a separate t1 and t2 thread
       for (int i = 0; i < 5; i++)
       {
           System.out.println("Thread Name "+Thread.currentThread().getName() + " executed with " + i + " loop.");
       }
   }
}

class B
{
   public static void main(String[] args) 
   {
       A obj = new A();
       
       // Create a Thread object and pass the instance of A
       Thread t1 = new Thread(obj);
       Thread t2 = new Thread(obj);
       
       // Start the t1 and t2 thread
       t1.start();
       t2.start();
   }
}

Output:

Thread Name Thread-0 executed with 0 loop.
Thread Name Thread-1 executed with 0 loop.
Thread Name Thread-0 executed with 1 loop.
Thread Name Thread-1 executed with 1 loop.
Thread Name Thread-0 executed with 2 loop.
Thread Name Thread-1 executed with 2 loop.
Thread Name Thread-0 executed with 3 loop.
Thread Name Thread-1 executed with 3 loop.
Thread Name Thread-0 executed with 4 loop.
Thread Name Thread-1 executed with 4 loop.

Loading

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.