Interface in java with example

We are going to discuss about Interface in Java. Interface is blueprint of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended it by other interface.

Interface in java with example

We are going to discuss about Interface in Java. Interface is blueprint of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended it by other interface.

Interface in java with example

We are going to discuss about Interface in Java. Interface is blueprint of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended it by other interface. We can extend one or more other interfaces but cannot implement them. Java interface is nothing but is a empty collection, implementation and constructs variables than static and final declaration. We can declare all method in interface abstract and public. Multiple inheritance allows extending interface.

Why use interface in java?

  • We use itto achieve complete abstraction.
  • The interface, we can support the functionality of the multiple inheritance.
  • we can use it to achieve loose coupling.
  • We can implement all method heading listed in interface.
  • We can take the interface check, that implements a empty method message().

How to create interface in java program: Following is the code of interface in java program. We have created interface method in java using "interface" keyword.

package Interface;

interface Demo {
     void call();
   
      }
interface Demo1{
	 void run();
 }
 
class CallTest implements Demo{
    public void call() {
		System.out.println("Hello java");
		
	}
}
 class RunTest implements Demo1{
	 public void run(){
		 System.out.println("Hello interface");
	 }
 }
 
 public class interfaceDemo{
	 public static void main(String[] args) {
		Demo demo = new CallTest();
		Demo1 demo1 = new RunTest();
	      demo.call();
	      demo1.run();
	}
 }

OutPut:-

Download Source Code