X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Interface :


  • Interfaces are basically used to develop user defined data types.

  • With respect to interfaces we can achieve the concept of multiple inheritances.

  • With interfaces we can achieve the concept of polymorphism, dynamic binding and hence we can improve the performance of a Java program in terms of memory space and execution time.

  • An interface contains the collection of purely undefined methods or an interface is a collection of purely abstract methods.

  • We can achieve security because we are not highlighting our internal implementation.

  • Enhancement will become easy because without effecting outside person we can change our internal implementation.

  • Two different systems can communicate via interface (eg. a Java application can talk with mainframe system through interface).

Syntax for defining an interface: interface <interface name> { Variable declaration; Method declaration; }


  • interface is a keyword which is used for developing user defined data types. Interface name represent a Java valid variable name and it is treated as name of the interface. With respect to interface we cannot create an object directly but we can create it indirectly. Variable declaration represents the type of data members which we use as a part of interface. Whatever the variables we use in the interface are meant for general purpose (the variables like PI, etc.).




Whatever the variables we write in the interface, they are by default belongs to :

public static final xxx data members

(xxx represents data type, variable name and variable value)





  • All variables must be initialized (otherwise it will be compilation error). Method declaration represents the type of methods we use as a part of interface. All the methods of interface are undefined methods and to make those methods as abstract, the Java programmer will not need to write a keyword 'abstract' explicitly before the declaration of interface method. Since all the methods of interface are meant for general purpose hence they must give universal access. To make it as universal access the Java programmer will not need to write a keyword 'public' explicitly before the method declaration. Hence, by default all the methods of interfaces belong to public abstract methods.

Example:

interface i1 { int a; //invalid since, initializing is mandatory int b = 20; void f1 (); void f2 (); }


Note:

  • Whenever we compile an interface, we get <interface name>.class as an intermediate file, if no errors are present in interface.

Syntax-1 for reusing the features of interface(s) to class: [abstract] class <class_name> implements <interface 1>,<interface 2>.........<interface n> { variable declaration; method implementation; }; In the above syntax class_name represents name of the class which is inheriting the features from 'n' number of interfaces. 'implements' is a keyword which is used to inherit the features of interface(s) to a derived class.


Note:

  • When we inherit 'n' number of abstract methods from 'n' number of interfaces to the derived class, if the derived class provides definition for all 'n' number of abstract methods then the derived class is known as concrete class. If the derived class is not providing definition for even one abstract class then it must be made as abstract by using a keyword 'abstract' i.e. if a class implements an interface then it is compulsory to provide implementation for every methods of that interface otherwise we have to declare class as abstract. If we won't do so then we will get compile time error.

  • Whenever we are implementing an interface's methods in our class then it is compulsory that those methods should be declared as public otherwise we will get compile time error.

  • One class can extend only one class at a time.

  • One class can implement 'n' number of interfaces at a time.

  • One interface can extends more than one interface simultaneously.

  • Interface cannot implements or extends a class. Since defined things cannot be made as undefined things.

Syntax-2 inheriting 'n' number of interfaces to another interface: interface <interface_name> extends <interface 1>,<interface 2>.........<interface n> { variable declaration cum initialization; method declaration; };


Example:

interface I1 { int a = 10; void f1 (); }; interface I2 extends I1 { int b = 20; void f2 (); };


  • If one interface is taking the features of another interface then that inheritance is known as interface inheritance.

  • An interface can extend 'n' number of interfaces at a time.

Syntax-3: [abstract] class <derived class_name> extends <base class_name> implements <interface 1>,<interface 2>.........<interface n> { variable declaration; method definition or declaration; };


  • Whenever we use both extends and implements keywords as a part of Java program we must always write extends keyword first followed by implements keyword.

Important points:


  • An object of interface cannot be created directly since it contains 'n' number of abstract methods. An object of interface can be created indirectly. An object of interface = an object of that class which implements that interface.




  • An object of base interface contains the details about those methods which are declared in that interface only but it does not contain details about those methods which are specially available in either in derived classes or in derived interfaces.




  • An interfaces should not be final.




  • An interface does not contain Constructors.




  • Inside interface we can't take instance and static block.




  • Whether we are declaring or not, every interface method is public and abstract.




  • The following method declarations are equal inside interface :

void m1();
public void m1();
abstract void m1();
public abstract void m1();
  • The following modifiers are not applicable for interface methods : private, <default> , protected, final, static, strictfp, synchronized, native.




  • An interface can contain variables which specify constants at requirement level.




  • Every interface variable is always public, static and final whether we are declaring or not.




  • The following variable declarations are equal inside interface :

int x = 10;
public int x = 10;
public static int x = 10;
public static final int x = 10;
public static int x = 10;
final int x = 10;
public final int x = 10;
static final int x = 10;
  • The following modifiers are not applicable for interface variables : private, <default> , protected, volatile, transient.




  • For the interface variable it is compulsory that we should perform initialization at the time of declaration only otherwise we will get compile time error.




  • Inside implementation classes, we can access interface variable but we can't modify their values.





  • Interface naming conflicts :


    Method naming conflicts :-


  • If two interfaces contains a method with same signature and same return type then in the implementation class, we can provide implementation of only one method.

interface I1
{
 public void m1();
}

interface I2
{
 public void m1();
}

class Example implements I1 , I2
{
 public void m1()
 {
 
 }
}
  • If two interfaces contain a method with same name but different arguments then in the implementation class we have to provide implementation for both methods and these methods are considered as overloaded methods.

interface I1
{
 public void m1();
}

interface I2
{
 public void m1(int i);
}

class Example implements I1 , I2
{
 public void m1()
 {
 
 }
 public void m1(int i)
 {
 
 }
}
  • If two interfaces contain a method with same signature but different return type then it is not possible to implements both interface at a time. We can't write any Java class which implements both interface simultaneously. It is true that a Java class can implements any number of interfaces simultaneously except if two interfaces contain a method with same signature but different return types.

interface I1
{
 public void m1();
}

interface I2
{
 public int m1();
}

Variables naming conflicts :-


  • There may be a chance of two interfaces contain variable with same name and may rise variable naming conflict but we can resolve this naming conflict by using interface names to access variables.

Example 1 :

interface I1 { int x = 10; } interface I2 { int x = 20; } class Example implements I1 , I2 { public static void main(String [ ] args) { System.out.println(x); } }

Output:

Compile time error : reference to x is ambiguous, both variable x in I1 and variable x in I2 match

Example 2 :

interface I1 { int x = 10; } interface I2 { int x = 20; } class Example implements I1 , I2 { public static void main(String [ ] args) { System.out.println(I1.x); System.out.println(I2.x); } }

Output:

10 20

Marker Interface :


  • If an interface won't contain any method and by implementing that interface if our objects will get some ability then such type of interface is called marker interface or tag interface or ability interface. Example : Serializable, Cloneable, RandomAccess etc. By implementing Serializable interface we can send object across the network and we can save the state of the object to a file. This extra ability is provided through Serializable interface. By implementing Cloneable interface our objects will be in position to provide exact duplicate object.

  • JVM is responsible to provide required ability in marker interface even though it don't contain any method which reduces complexity of the programmer.

  • We can create our own marker interface but customization of JVM will be required.


Adapter class :


  • Adapter class is a simple Java class that implements an interface and provide empty implementation.

interface I1
{
 public void m1();
 public void m2();
 public void m3();
}
abstract class Adapter implements I1
{
 public void m1()
 {
 
 }
 public void m2()
 {
 
 }
 public void m3()
 {
 
 }
}
  • If we implement an interface directly then it is compulsory that we should provide implementation for every methods of that interface whether we are interested or not and whether it is required or not. It increases the length of the code and also reduce the readability of code.

class Example implements I1
{
 public void m1()
 {
 
 }
 public void m2()
 {
 
 }
 public void m3()
 {
 
 }
}
  • If we extends Adapter class instead of implementing interface then we have to provide implementation only for required method but not for all methods which reduce length of the code and it's readability.

class Example extends Adapter
{
 public void m3()
 {
 
 }
}


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.