Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 3 April 2017

Interface Java

 

Java Interface

An interface is a blueprint of a class in java. An interface in java is a collection of public static final variables (constants) and abstract methods.

The main purpose of using interface in java is to achieve fully abstraction and multiple inheritance. Because we cannot achieve multiple inheritance through class.


Use of Interface in Java

There are many reasons to use interface in java programming language. 
  • It is used to achieve fully abstraction in java
  • By using interface, you can achieve multiple inheritance in java which is not possible through class.
  • It can be used to achieve loose coupling.

Some points about interface

  • You cannot create an instance of an interface just like an abstract class.
  • In an interface in java all the data members or variables are public , static and final by default.
  • In an interface in java all the methods are public and abstract by default.
  • Interface are predefault abstract so there is no need to use abstract keyword when declaring an interface.

Compiler's behavior with interface programs

The java compiler adds public, static and final keyword before data members  and public and abstract keyword before the methods in interface. we can see in below example :
Interface Java
In other words in an interface data members are by default public, static and final and methods are public and abstract only.


How to declare an interface

We can declare interface by using "interface" keyword.

Syntax :

interface Student
{
int age = 25;//public, static, final by default
void show();//public and abstract by default
}


Relationship between classes and interfaces in java

We can see in below diagram, a class extends another class and a class implements an interface and an interface extends another interface and an interface extends more than two interface simultaneously.
java interface

 

Java Interface Example

In the below example, there is Information interface with one method massage() and its implementation is provided in the the class First.

interface Information
{
void massage();
}
class First implements Information
{
public void massage()
{
System.out.println("Ok");
}
public static void main(String args[])
{
First f = new First();
f.massage();
}


ouput : Ok


Another Example of Java Interface

In the below example, Sketch interface has only one method and its implementation is provided by Men and Women classes. If we understand, interface is created by someone but implementation provided by different implementation providers and it is used by  someone else. The implementation part is hidden by the user which uses the interface.

File : InterfaceTest.java

//interface declaration by the first user

interface Sketch
{
void draw();
}

//implemetations by second user

class Men implements Sketch
{
public void draw()
{
System.out.println("Sketch of Men");
}
}
class Women implements Sketch
{
public void draw()
{
System.out.println("Sketch of Women");
}
}

//interface used by third user

class InterfaceTest
{
public static void main(String args[])
{
Sketch s = new Women();
s.draw();
}
}

output : Sketch of Women


Multiple Inheritance in Java by Interface

If a class implements multiple interface simultaneously or an interface extends multiple interface simultaneously, is known as multiple inheritance in java.

For example :

In this example, a class implements multiple interface.

interface Bike
{
void speed();
}

interface Cycle
{
void run();
}

class Vehicle implements Bike,Cycle
{
public void speed()
{
System.out.println("Bike's speed is fast");
}
public void run()
{
System.out.println("Cycle run slow");
}
public static void main(String args[])
{
Vehicle v = new Vehicle();
v.speed();
v.run();
}


output : Bike's speed is fast
              Cycle run slow

For example 2 : 

In this example, a class implements an interface but one interface  extends another interface in java.

interface Vehicle
{
void speed();
}

interface Car extends Vehicle
{
void speedTest();
}

class Simple implements Car
{
public void speed()
{
System.out.println("Less than 40");
}
public void speedTest()
{
System.out.println("More than 60");
}
public static void main(String args[])
{
Simple s =
new Simple();

s.speed();
s.speedTest();
}
}

output : Less than 40
              More than 60


Default Method in Interface (Java 8)

Since Java 8, we can give the method body in an interface but we have to make it default method.

For example :

interface Animal
{
void eat();
default void sleep()
{
System.out.println("Animal is sleeping");
}

class DefaultMethod implements Animal
{
public void eat()
{
System.out.println("Animal is eating");
}
public static void main(String args[])
{
DefaultMethod dm = new DefaultMethod();
dm.eat();
dm.sleep();
}
}

output : Animal is eating
              Animal is sleeping


Marker Interface in Java

An interface that have no members is called marker or tagged interface in java e.g Serializable interface, cloneable interface etc.

For example : 

public interface Serializable
{
//No members in an interface
}

Nested Interface in Java

An interface that contains another interface is called nested interface in java. We will learn in details in nested classes.

For example :

interface First
{
void work();
interface Second
{
void task();
}
}
Share:

1 comment:

  1. Nice tutorial on java interface with simple and useful examples

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate