on Leave a Comment

Interface in Java With Examples

In java, full abstraction can be achieved by interface. An interface is like a class, but it is very different from class. Like class, interface also contains variables and methods. But interface defines only final fields and abstract methods. In class, by default all members are of package level access modifier or default modifier. But in interface, all members are of public access modifier ad we can't change it. In interface, all variables are public, static and final and all methods are public and abstract.

Uses of Java Interface

It is used to achieve full abstraction.
To support the functionality of multiple inheritance.
It is also used to achieve loose coupling.

Syntax of Java Interface

interface InterfaceName
{
 //Variables Declaration
 //Methods Declaration
}

Example:

interface Example
{
    int a = 10;
    void show();
}

In interface, methods are declared without any body.

Example of Interface in Java

interface Example
{
 void setName(String n);
 void setAge(int x);
 /*
  Compiler will convert it as
  public abstract void setName(String n);
  public abstract void setAge(int x);
    */
}
class Student
{
 String name;
 int age;
 int rollno;
 void setName(String n)
 {
  name = n;
 }
 void setAge(int x)
 {
  age = x;
 }
 void setRollno(int r)
 {
  rollno = r;
 }
    void showDetails()
    {
     System.out.println("Name: "+name);
     System.out.println("Age: "+age);
     System.out.println("Roll no: "+rollno);
    }
}
class Testing
{
 public static void main(String[] args) {
  Student s = new Student();
  s.setName("John");
  s.setAge(17);
  s.setRollno(28);
                s.showDetails();
 }
}

Output:

Name: John
Age: 17
Roll no: 28

Interface and Inheritance

We know that a class cannot extents more than one class, but it can implements one or more interfaces. Like a class extends other class, an interface extends other interface.


Example 1:

interface I1
{
 public void display1();
}
interface I2
{
 public void display2();
}

class Testing implements I1,I2
{
 public void display1()
 {
  System.out.println("Interface I1");
 }
 public void display2()
 {
  System.out.println("Interface I2");
 }
 public static void main(String[] args) {
  Testing t = new Testing();
  t.display1();
  t.display2();
 }
}

Output:

Interface I1
Interface I2

Example 2:

interface I1
{
 public void display1();
}
interface I2 extends I1
{
 public void display2();
}

class Testing implements I2
{
 public void display1()
 {
  System.out.println("Interface I1");
 }
 public void display2()
 {
  System.out.println("Interface I2");
 }
 public static void main(String[] args) {
  Testing t = new Testing();
  t.display1();
  t.display2();
 }
}

Output:

Interface I1
Interface I2

Why we cannot create instance of an interface?

We know that variables are by default static in an interface. And static variables do not belongs to object. This is the reason why there is no constructor in interface. We already know that when an object creates constructor automatically calls. So interface does not have constructor and instance variable, this is the reason why we cannot create instance of an interface.






0 comments:

Post a Comment