Saturday, 22 April 2017

Interfaces

Let us discuss one of the interesting concepts of java,interfaces.Interfaces are the extension to the abstract classes.Interfaces are the block of statements that contain only abstract methods.

How interfaces are used ?


If you want to use a block of statements under interface,we use the keyword 'interface' before its name.
interface name
{ 
 methods without implementation
}

How to access methods in interfaces:


methods declared under interfaces can be accessed in two ways.

Creating object to the class:


The most general method of accessing the interfaces are by creating an object for the class that is implementing the interfaces.



Creating a reference for the interfaces:


In this method,we create reference to the interface and call the appropriate class that implements the interfaces.

Example:

import java.io.*;
interface Program
{ 
  void methodA();
  void methodB();
}
class Code implements Program
{ 
 public void methodA()
 {
  System.out.println("methodA is implemented in class Code");
 }
 public void methodB()
 {
  System.out.println("methodB is implemented in class Code");
 }
}
class Titans implements Program
{
 public void methodA()
 { 
  System.out.println("methodA is implemented in class Titans");
 }
 
 public void methodB()
 { 
  System.out.println("methodB is implemented in class Titans");
 }
}
class MainClass
{ 
 public static void main(String args[])
 { //accessing the interface method implemented in Code class
  Code c=new Code();
  c.methodA();
 
  //accessing the interface method implemented in the Titans class
  Program ref;//creating reference to the interface
  ref=new Titans();
  ref.methodB();
 }
}

Output:

methodA is implemented in class Code
methodB is implemented in class Titans

Implementing multiple inheritance using interfaces:


Multiple inheritance is not available in java,but we can achieve it by using interfaces.Multiple inheritance is nothing but a class containing more than one parent classes.

Why java does not support multiple inheritance?


when we have two parent classes that contain same variable names and method names then,whenever some method is called there exists a ambiguity in invoking the desired method. We can overcome this problem using interfaces consider the following interfaces

interface A
{ 
 int x=5;
 void methodA();
}
interface B
{ 
 int x=5;
 void methodA(); 
}

A class implements these interfaces To access interface A's x,we write A.x To access to interface B's x,we write B.x From this,ambiguity is resolved. Example :

import java.io.*;
interface Mother
{ 
 int age=30;
 void childAge(){}
}
interface Father
{ 
 int age=40;
 void childAge(){}
}
class Child implements Father,Mother
{ 
 public void childAge()
 { 
  float cage;
  cage=(Father.age+Mother.age)/2;
  System.out.println("child age is"+cage);
 }
}
class MainClass
{
 public static void main(String args[])
 {
 Child c=new Child();
 c.childAge();
 }
}

Output:

child age is 35.0

Points to be noted:

  • We cannot instantiate an interface.
  • Interfaces will provide 100% abstraction.
  • The methods in the interfaces are default to abstract and public type.
  • Interfaces cannot be protected or private.
  • variables declared in interfaces are public static final variables.
  • Interface variables must be initialized at the time of declaration,otherwise compiler throws error.
  • We cannot reinitialize the interface variables.
  • Any interface can extend any other interface but cannot implement them.

No comments:

Post a Comment