• We Code
  • We Design
  • We Develope
  • We Write
  • We Share

menu

Tuesday, September 11, 2012

type of classes -interface



Interface

Interface in java programming language  is equivalent to class , but it is a pure abstract class.

There are many benefits of interface in java programming language but the current topic doesn't  fit to explain about all practical benifits of interfce. Hence we can briefly discuss about interface here.


  1. All methods in the interface are by default declared as abstract.
  2. We can declare only method with parameters and return type alone in interface
  3. we cannot write any method with implementation in interface.
  4. when method do't have body such methods are declared as abstract.
  5. While declaring interface,in place of class keyword we must use interface keyword.



For Examples :
  public interface Myinterface
              {
               
               }

Interface variables are by default declared as static and final.The reason is to avoid/prevent runtime variable ambiguity problem.


It can be described as before other OOP languages has support for multiple inheritance . In multiple inheritance in the same variable derives from more than one super class,suppose in future if we refer super.var in sub class,at runtime the runtime environment detect and asks that the variable came from two super classes from which super class the variables want to be taken into consideration . It is a ambiguous problem.


But whereas in java the variables of interfaces are final and static by default .Suppose class B derives  from super class A,and also derives from interfce C and D. In the  same variable derives from both super class and from one super interface,the recent super interface variables overrider class variable and also becomes final. If class B derives from another interface also that contains the same variable compiler detect ambiguous error at compilation time itself . Hence variable ambiguity is solved at the time of compilation .
Variables which are declared as final are not modifiable but if they are not declared as static ,for every instance of interface (means its sub class) new memory allocates to variable that leads to duplication of memory consumption.

To avoid unnecessary memory consumption interface variables are declared as static so that to any number instance of a sub class only one copy of memory is allocated static variables.
that's why interface variables are static and final.





//Ainterface.java
public interface Ainterface
  {
   
   public static final int i=10;
  } 



//Binterface.java

public interface Binterface
  {
   
   public static final int i=10;
  } 

0 comments:

Post a Comment

...