Sunday, October 24, 2021

Interface in java-AapKiApniSchool


Interface in java: 

interface is a blueprint of class in java that is used to define contracts. It can contain static constant and abstract method. Interface is used in java for achieving abstraction. With the help of interface we can achieve multiple Inheritance. Till java 7 we can't create methods with body. All the methods will be abstract public.


In java 8 we can create default and static methods with body.

In java 9 we can create private method also.


Uses of Interface:


1. For achieving multiple Inheritance.

2. For achieving loose coupling.

3. For achieving abstraction.

4. Interface is used for abstraction.Many people thinks that why we use interface when we have abstract class. Abtract class is heavy weight as it can contain non-final variable,non-abstract method,constructors alos but Interface methods are public abstract and all variables are public static final.


How to declare an interface?

We declare interface in java with Interface keyword. By default all the variables in interface are public static final and all methods are public abstract.

Syntax:


import java.lang.*;
// Any number of import statements

public interface NameOfInterface {
   // Any number of final, static fields
   // Any number of abstract method declarations\
}


New Features in JDK8 Interface:


1. Prior to Java8 we could not able to add implementation in Interface. From Java8 we can provide some default implementation with default method. Default implementation has special use.It doen't affect the abstraction behind the Interface.
Suppose we want to provide new functionality in existing interface then it will not work as once we will create any method then all implementations class need to provide implementation those class need this functionality or not.
2. We can create static method in Java8 Interface. This static method we can access by this interface name only. For calling static method of Interface we don't need any object of implementation class.


Java 8 Default Method in Interface:


  // An example to show that interfaces can
// have default methods from JDK 1.8 onwards
interface In1
{
    final int a = 10;
    default void display()
    {
        System.out.println("Default Method");
    }
}
  
// A class that implements the interface.
class MainClass implements In1
{
 
    public static void main (String[] args)
    {
        MainClass t = new MainClass();
        t.display();
    }
}


Output :
Default Method


Java 8 Static Method in Interface:


 
 // An example to show that interfaces can
// have static methods from JDK 1.8 onwards
interface In1
{
    final int b = 10;
    static void message()
    {
        System.out.println("Static Method");
    }
}
  
// A class that implements the interface.
class MainClass implements In1
{
   
    public static void main (String[] args)
    {
        In1.message();
} }


Output :
Static Method


New Features in JDK9 Interface:

1. We can create private method.
2. We can create private static method.

Marker or tagged interface:

An interface which don't have any any field or methods. An empty interface is called marker interface which is used to provide some special information to JVM. Some Marker interface is used very frequently like Serializable, Cloneable and Remote.

  public interface Serializable 
{
  // Empty,no fields and no methods
}
  

Serializable interface:

Serializable interface is a marker interface which is part of Java.io package. A class that implements this interface that the capabilty to save the state of that class objet in file, without implementing this class, class can't save state of object in file.

Implementing Interfaces:

When a class is implementing Interface that class has to provide implementaion of all the methods of interface. If class is not providing implementation then that class has to declare that method as abstract.
A class use the implements keyword.

interface Printable{  
void print();  
}  
 
class Test implements Printable{  
public void print(){System.out.println("Printing Data");}  
  
public static void main(String args[]){  
Test obj = new Test();  
obj.print(); } }

Output :
Printing Data

Rules For Implements Interface:

1. A Class can implenets multiple interfaces.
2. A class can extends only one class but can implemets multiple interfaces at a time.
3. An interface can extends another interface also.
4. An implementation class itself can be abstract and if so, interface methods need not be implemented.
5. Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.

No comments:

Post a Comment

Featured Post

Interface in java-AapKiApniSchool

Interface in java:  interface is a blueprint of class in java that is used to define contracts. It can contain static constant and abstract ...

Popular Posts