Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Interface Introduction

  • • An Interface is nothing but a kind of class.
  • • Like classes, interface also contains methods and fields but with a major difference.
  • • The difference is whatever the interface containing data, that is public, static and final at the time of compilation by default and whatever the interface containing method, that is public and abstract by default.
  • • So the interface in java is a mechanism to achieve fully abstraction.

Why Interface:

  • • To support the concept of functionality of multiple inheritance.
  • • To achieve fully abstraction, because by default the method in interface is public and abstract.
  • • The most important point is interface achieves loose coupling, which is the main advantage of interface.

The general form of an interface is

interface InterfaceName
{
	fields declarations;
	methods declarations;
}

The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.


Example

Interface Demo
{
	int x=10;
	void show();
}

Here the keyword interface tells that Demo is an interface containing one final field x and one abstract method show().

NOTE:
Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.

What we declare

interface Demo
{
	int x=100;
	void print();
}

What the Compiler Understand

interface Demo
{
	public static final int x=100;
	public abstract void print();
}

Let's see a demo program for better understanding:

Example;

InterfaceExample1.java

package java8s;

interface Demo
{
	void print();
}
class Example2 implements Demo
{
	 public void print()
	 {
		  System.out.println("JAVA means Silan Software");
	 }
}
public class InterfaceExample1 {

	 public static void main(String[] args) {
		 
		  Example2 obj=new Example2();
		  obj.print();
	 }
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext