• August 25, 2014
  • Core Java
  • Comments Off on Interface in java

Interface in java

Interface in real world:

You can see number of interface examples. Let us take example of a TV. You press change channel button of TV remote and channel is changed. Here remote is act as an interface between you and TV.

Dictionary meaning of interface:

A point where two systems, subjects, organizations, etc., meet and interact.

:

Interface is a way of implementing 100% abstraction. An interface is declared with interface keyword. It can contain only abstract methods and static final data members Or Interface is a group of related abstract methods.

Syntax:

Interface Interface_Name {

                //abstract methods

                //static final data members

}

An interface can’t be instantiated, it can be implemented by classes. To implement an interface implements keyword is used. Interface forms a contract with your class that force your class to have all methods defined by the interface must appear in the class. This all enforce check is done at compile time by compiler i.e. A class that implements an interface must implement all of the methods described in the interface.

Syntax:

class class_name implements Interface_Name {

                //all methods of interface

                //block of code for class

}

Note: 
1. A class extends another class.
2. An interface extends another interface.
3. A class implements an interface. 

Example:

/**
 * This program is used to show simple interface example.
 * @author javawithease
 */
interface ShowDetails{
                //This method is used to print name and age.
                void showDetail(String name, int age);
}
 
public class InterfaceExample1 implements ShowDetails{
                /**
                 * This method is used to print name and age.
                 * @author javawithease
                 */
                @Override
                public void showDetail(String name, int age) {
                       System.out.println("Name = " + name);
                       System.out.println("Age = " + age);
                }               
 
                public static void main(String args[]){
                        //object creation
                        InterfaceExample1 obj = new InterfaceExample1();
                        //method call
                        obj.showDetail("jai", 26);
                }
}

Output:

Name = jai
Age = 26

Download this example.

Multiple inheritance in java.

  1. 1. A class can implements multiple interfaces.
  2. 2. An interface can extends multiple interfaces.

 Example:

/**
 * This program is used to show multiple inheritance example.
 * @author javawithease
 */
interface ShowAge{
          //This method is used to print age.
          void age(int age);
}
 
interface ShowName{
          //This method is used to print name.
          void name(String name);
}
 
public class InterfaceExample2 implements ShowAge, ShowName{
                /**
                 * This method is used to print age.
                 * @author javawithease
                 */
                @Override
                public void age(int age) {
                        System.out.println("Age = " + age);
                }
 
                /**
                 * This method is used to print name.
                 * @author javawithease
                 */
                @Override
                public void name(String name) {
                       System.out.println("Name = " + name); 
                }                               
 
                public static void main(String args[]){
                        //object creation
                        InterfaceExample2 obj = new InterfaceExample2();
 
                        //method call
                        obj.name("jai");
                        obj.age(26);
                }
}

Output:

Name = jai
Age = 26

Download this example.

Why multiple inheritance is possible in case of interfaces but not possible in case of classes?

Multiple inheritance is possible in case of interfaces but not possible in case of classes because there is no ambiguity problem in case of interfaces as implementation is provided by class that implements interfaces not by interfaces itself.

Example:

/**
 * This program is used to show multiple inheritance is
 * possible in case of interfaces because there 
 * is no problem of ambiguity.
 * @author javawithease
 */
interface Show{
          //This method is used to print name and age.
          void show(String name, int age);
}
 
interface Detail{
          //This method is used to print name and age.
          void show(String name, int age);
}
 
public class InterfaceExample3 implements Show, Detail{
                @Override
                public void show(String name, int age){
                        System.out.println("Name = " + name);
                        System.out.println("Age = " + age);
                }                               
 
                public static void main(String args[]){
                       //object creation
                       InterfaceExample3 obj = new InterfaceExample3();
                       //method call
                       obj.show("jai", 26);
                }
}

Output:

Name = jai
Age = 26

Download this example.

An interface extends another interface Example:

/**
 * This program is used to show that an interface
 * can extends multiple interfaces.
 * @author javawithease
 */
interface ShowAge{
          //This method is used to print age.
          void age(int age);
}
 
interface ShowName{
          //This method is used to print name.
          void name(String name);
}
 
interface showDetails extends ShowAge, ShowName{
                //This method is used to print roll no.
                void rollNo(int rollNo);
}
 
public class InterfaceExample4 implements showDetails{
                /**
                 * This method is used to print age.
                 * @author javawithease
                 */
                @Override
                public void age(int age) {
                       System.out.println("Age = " + age);
                }
 
                /**
                 * This method is used to print name.
                 * @author javawithease
                 */
                @Override
                public void name(String name) {
                       System.out.println("Name = " + name);
                }               
 
                /**
                 * This method is used to print rollNo.
                 * @author javawithease
                 */
                @Override
                public void rollNo(int rollNo) {
                       System.out.println("RollNo = " + rollNo);
                }                               
 
                public static void main(String args[]){
                       //object creation
                       InterfaceExample4 obj = new InterfaceExample4();
 
                       //method call
                       obj.name("jai");
                       obj.age(26);
                       obj.rollNo(4);
                }
}

Output:

Name = jai
Age = 26
RollNo = 4

Download this example.

Methods of an interface are public abstract by default.

Data members of an interface are public static final by default.

Interface is abstract by default that’s why no abstract keyword is used in interface declaration.

Marker/Tagging Interfaces:

An interface with no methods is known as marker or tagged interface.

Why marker interface used:  

It provides some useful information to JVM/compiler so that JVM/compiler performs some special operations on it. It is used for better readability of code.  Example: Serializable, Clonnable etc. 

Syntax:

public interface Interface_Name {

}

 
How to create custom marker Interface.

Why an interface can’t have constructor?

Because interface not have any instance fields so nothing to construct. Now the question arise how interface can be inherited without constructor because subclass constructor call super class constructor. Now we have two cases. First case is when an interface extends other interface, in this case there is no issue because no interface have constructor and hence no super class constructor call. Second case when a class implements an interface, in this case there is no inheritance because class implements interface not extends interface.
 
Next Topic: Custom marker Interface in java with example.
Previous Topic: Abstract class in java with example.

Related Topics:

Command line arguments in java with example.
How to execute command line argument program in eclipse.
How to read input from command line in Java using Scanner.
Abstract class in java with example.
Interface in java with example.
Custom marker Interface in java with example.
Constructor in java with example.

 


Comments are closed.