Java Interface

 INTERFACE: 

  • An Interface In java is a blueprint of a class.
  • Interface contain final and static variables by default (Static: the variable can be directly accessed with-out creating an object that is a common Variable for all the class and Final: that variable value is constant we cannot change “ We no need to specify static, final keywords by default the variable treated as static & final).
  • Interface contain Abstract Methods(Abstract Method: the method which contain only definition not the body ” We can’t implement the body ”).
  • Methods in Interface are Public(access specifier) by default.
  • Interface supports the functionality of Multiple Inheritance.
  • We can define Interface with Interface keyword.
  • A class extends another class(by using extend keyword), An Interface extends another Interface but A class Implements an Interface(only interface without implementation-class(A class implements the interface with Implements key word) there is no use).
  • We can create Object reference for Interface but We can not instantiate Interface.

Example:


  interface A 

{

int a=10; // by default variables in interface are static and final

void m1(); // abstract method, by default public

}


 public class Test implements A

{

public void m1()

{

System.out.println(a);

}


public static void main(String[ ] args) 

{

/* Test t=new Test() ; // this is one way of creating object through class ‘Test’ and                                             calling method “m1()” through that Object.

t.m1(); */


A a=new Test(); // through Interface we can create object reference but we can                                         not instantiate  so we use Test class here instead of interface.

a.m1() ;

}

}

Comments

Popular posts from this blog

Data-sly-list & Data-sly-repeat

Java HashMap

Java Packages