What is Interface in Java With Example Program | Full Stack With Java

Interface in Java: An Interface Can Consist of Only Incomplete Methods or Abstract Methods in it.

Interface Example 1

public interface A {
public void test() {    // Error
}
}

Interface Example 2

public interface A {
public void test();   // correct
}

Interface in Java Important Points

#01 When A class implements An Interface then it Means We Are Inheriting Incomplete Method From Interface to Class and Than in Class We Override and Complete that Method.

Example 1

Package p1
public interface A {
public void test();
}
Package p1
public class B implements A {
@Override
public void test() {
System.out.println(100);
}
public static void main ( String[] args ) {
B b1 = new B();
b1.test();
}
}
Output :
 100

Example 2

Package p1
public interface A {
public void test1();
public void test2();
}
Package p1
public class B implements A {
@Override
public void test1(){
System.out.println(10);
}
@Override 
public void test2(){
System.out.println(20);
}
public static void main(String[] args ) {
B b1 = new B();
b1.test1();
b1.test2();
}
}

Output :
10
20

You Also Learn –

Note – Interface supports Multiple inheritances But Class Not Support Multiple inheritances.

Multiple Inheritance in Interface

Interface in Java
Multiple Inheritance in interface

Example 1

public interface A {
public void test1();
}
public interface B {
public void test2();
}
public interface C extends A,B {
}
public class D implements C  {
@Override
public void test1()  {
System.out.println( 100);
}
@Override
public void test2()  {
System.out.println( 200);
}
public static void main(String[] args ) {
D d1 = new D();
d1.test1();
d1.test2();
}
}
Output : 
100
200

Functional Interface in Java 8 With Example

Functional Interface in Java : – A functional interface can Consist of exactly one incomplete method in it.

Example 1

@FunctionalInterface
public interface A {      // Correct
public void test();
}

Example 2

@FunctionalInterface
public interface A {      // Error
public void test1();
public void test2();
}

Note – Functional Interface Was Introduced in Version 8 of java.

  • Functional Interfaces Can Be Implemented by a Lambda Expression.
  • We Cannot do Multiple inheritances on the Functional Interface.
  • We Can Create Any No. of Complete Methods inside Functional Interface.

Interface in Java Program

Note 1 – All the variables created interface by default final and static.

public interface A {
	int x=10;    // final and static
	
}
public class B {
public static void main(String[] args) {
	System.out.println(A.x);
}
}
Output: 10

Note 2 – An object of an interface can not be created.

public interface A {
	int x=10;
}
public class B {
public static void main(String[] args) {
	A a1 =new A(); // Error
	System.out.println(A.x);
}
}

Note 3 – A reference variable of an interface can be created.

public interface A {
	int x=10;
}
public class B {
public static void main(String[] args) {
	A a1 =null;
	System.out.println(a1);
}
}
Output: 
null

Leave a Comment