http://www.technicalpage.net/search/label/SQL

>> interface


Interface in java

Interface is similar to class , but having some differences as mentioned below :
Interface can have methods and variables. All the methods inside interface are abstract methods , ie, method without definition. The methods are by default public so you do not need to mention public. You can write "public void methodName();" or "void methodName()".
The variable inside interface are by default public static and final , so the variable must be assigned a value. 
“interface” keyword is used to declare an interface and written as interface followed by the interface name, same like class where interface is used in the place of class and interfaceName is used in the place of className.

Syntax:
public  interface InterfaceName {
}

Examples:
public  interface  I1  
{
void mon();
void sun();
}

public  interface I2
{
int n = 10;
public  void wed();   //writing public is optional because by default it is public

}

public  interface I3
{
void  sat();  
}

An interface can be extended by another interface called inheritance in interface. Multiple inheritance is possible in interface unlike class.

Interface I4 extends I3
{
void thus();
}

Interface I5 extends I1 , I2
{
void fri();
}

We can implement interface by class. When interface is implemented by class, the class should use all the abstract method in that interface and may or may not define all the abstract methods of the interface in the class.  If a class is having at least one abstract method, whether the method is coming from interface or created within the class,  then the class is called abstract class. You can not create object of the interface since it has abstract methods . In the same way, if the class is abstract, object can not be created for that class also.

Let’s create a class C1.

Public class C1 {
}

Let’s implement interface i2 by class C1.

public class C1 implements I2 {
      
              public void wed()
              {
                     System.out.println("This is implementation of interface into class.");
                     System.out.println("value of n from interface I2 is = "+n);
              }
      
              //Let's run above method:
              public static void main(String[] Args) {
                     C1 o_C1 = new C1();
                     o_C1.wed();
              }     
}

Output after run:
This is implementation of interface into class.
value of n from interface I2 is = 10


Let's implement interface which extends another interface.

public class C2 implements I4 {
      
       public void thus() {
              System.out.println("This is implementation of interface I4 by class C2.");
       }
      
       public void sat() {
              System.out.println("Calling this method is necessary because I4 extends I3.");
       }

       //Let's run above methods:
       public static void main(String[] args) {
             
              C2 o_C2 = new C2();
              o_C2.thus();
              o_C2.sat();

       }

}

Output after run:
This is implementation of interface I4 by class C2.
Calling this method is necessary because I4 extends I3.

Let's implement interface which implements multiple interfaces:
public class C3 implements I5 {
      
       public void fri() {
              System.out.println("This is implementation of interface I5 by class C3.");
       }
      
       public void mon() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }

       public void sun() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }
      
       public void wed() {
              System.out.println("Calling this method is necessary because I5 extends I2.");
       }


       //Let's run above methods:
       public static void main(String[] args) {
             
              C3 o_C3 = new C3();
              o_C3.fri();
              o_C3.mon();
              o_C3.sun();
              o_C3.wed();

       }

}
Output after run:
This is implementation of interface I5 by class C3.
Calling this method is necessary because I5 extends I1.
Calling this method is necessary because I5 extends I1.
Calling this method is necessary because I5 extends I2.

A class having abstract method inside it is called abstract class:
If we do not define all the abstract methods from the interface inside the implementing class, or add any additional abstract method inside the class then the class becomes abstract class. If we try to use the abstract class as a normal class, we get error. We can not create object of the abstract class.

Abstract class example1:
public abstract class C3 implements I5 {
      
       public void fri() {
              System.out.println("This is implementation of interface I5 by class C3.");
       }
      
       public void mon() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }

       public void sun() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }
      
       public abstract void wed();
}

Abstract class example2:
public abstract class C3 implements I5 {
      
       public void fri() {
              System.out.println("This is implementation of interface I5 by class C3.");
       }
      
       public void mon() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }

       public void sun() {
              System.out.println("Calling this method is necessary because I5 extends I1.");
       }
}


A class  can implement more than one interfaces : say i1 and i2.

public class C3 implements I1 , I2 {

       public void mon() {
              System.out.println("Class implementing two interfaces, from I1");
       }

       public void sun() {
              System.out.println("Class implementing two interfaces, from I1");
       }
      
       public void wed() {
              System.out.println("Class implementing two interfaces, from I2");
       }
      
       //Let's run above methods:
       public static void main(String[] args) {
             
              C3 o_C3 = new C3();
              o_C3.mon();
              o_C3.sun();
              o_C3.wed();

       }
Output after run:
Class implementing two interfaces, from I1
Class implementing two interfaces, from I1

Class implementing two interfaces, from I2



You can add additional methods and/or variables in the class alongwith the methods coming from the implemented interface. If the value of  "n" (this variable has been assigned a value in interface i2) is again assigned a new value in the implementing class, then,   when called the value of "n" , we get the new value assigned in the class. To get the original value of  "n" from the interface, we need to call "n" this way interfaceName.Variable  such as in above example i2.n.


public class C3 implements I2 {

       // in I2 n=10 , let's give new value to n
       int n=100;
       public void wed() {
              System.out.println("Class implementing interface I2");
       }
      
       public void valueofn1() {
              System.out.println("This will print the value of n =100 , let's see");
              System.out.println("Value of n is : "+n);
       }
      
       public void valueofn2() {
              System.out.println("We can print the value of n from interface like below.");
              System.out.println("Value of n is : "+I2.n);
       }
      
       //Let's run above methods:
       public static void main(String[] args) {
             
              C3 o_C3 = new C3();
              o_C3.wed();
              o_C3.valueofn1();
              o_C3.valueofn2();

       }
  
}

Output after run:
Class implementing interface I2
This will print the value of n =100 , let's see
Value of n is : 100
We can print the value of n from interface like below.

Value of n is : 10



You can not create object of interface because you can not create constructor for interface.
So,
You can not create object of interface.
You can not create constructor of interface.

Interface can be used for security purpose also. For example,  when you implement interface , you have to define all the abstract methods of the interface into the implementing class as well as you can add additional new methods in the implementing class. If you create object of the class, that object can reference all the methods inside the class including the additional methods. But  if you want to run only specified methods, then mention the method in the interface and  create object and give memory of the class , right word would be , create reference of the interface and give memory of the class. So that , the object can be used for the methods which were implemented from the interface only.  This object can not be  used for  other additional methods inside the class.


public class C3 implements I3 {

       public void sat() {
              System.out.println("This class has additional methods");
              System.out.println("But only the method from interface can be made working, let's see.");
       }
      
       public void newMethod1() {
              System.out.println("additional method 1");
       }
      
       public void newMethod2() {
              System.out.println("additional method 2");
       }
      
       //Let's run above methods:
       public static void main(String[] args) {
             
              I3 o_I3 = new C3();
              o_I3.sat();   /*  Only this code will work.  */
             
/*            o_I3.newMethod1();   we get error if we try to use this.
              o_I3.newMethod2();   we get error if we try to use this.     */

       }

Output after run:
This class has additional methods
But only the method from interface can be made working, let's see.


Only Interface accepts Default access modifier:

Interface:
public interface InterfaceXYZ {

       default void red() {
              System.out.println("Default method in interface");
       }

Class implementing the interface:
public class XYZ implements InterfaceXYZ{

       public static void main(String[] args) {
             
              XYZ xyz = new XYZ();
              xyz.red();
             
       }

Output:

Default method in interface

Advantages of Interface:
It makes using multiple inheritance in java  possible
Interface add security as explained above.
 
Summary:
1. Interface have methods by default public so no need to use public word  and the methods are not defined.
2. Variables in interface are by default public static final , so must be initialized.
3. Class implements interface, if it does not define all the interface methods then  it becomes an abstract class.
4. We can create object of the class only when there is no undefined methods, ie we can not create object of the abstract class.
5. You can not create object of the interface or create constructor of the interface.
6. You can create object reference of the class to implement methods only from the interface. With object reference, you can not use methods from the class.
7. Multiple inheritance is possible in two ways: 
    1. One interface can extend more than one interface and the class implement the interface
    2. When a class implements more than one interface


No comments:

Post a Comment