Monday, August 12, 2019

OOPS – Interface in Java

Interface:


What is an interface in Java?
Interface looks like a class, but it is not a class. An interface can have methods and variables just like the class, but the methods declared in interface are by default abstract (only method signature, no body) Also, the variables declared in an interface are public, static & final by default.

Why do we use interface?
·        It is used to achieve total abstraction.
·        Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance.
·        It is also used to achieve loose coupling.
·        Interfaces are used to implement abstraction. So, the question arises why use interfaces when we have abstract classes?
·        The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.

What is the use of interface in Java?
As mentioned above they are used for full abstraction. Since methods in interfaces do not have body, they must be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, however you can implement more than one interfaces in your class.
Without bothering about the implementation part, we can achieve the security of implementation
Syntax:
Interfaces are declared by specifying a keyword “interface”. 

E.g.:
interface MyInterface
{
   /* All the methods are public abstract by default
    * As you see they have no body
    */
   public void method1();
   public void method2();
}



Example of an Interface in Java
interface MyInterface
{
   /* compiler will treat them as:
    * public abstract void method1();
    * public abstract void method2();
    */
   public void method1();
   public void method2();
}
class Demo implements MyInterface
{
   /* This class must have to implement both the abstract methods
    * else you will get compilation error
    */
   public void method1()
   {
               System.out.println("implementation of method1");
   }
   public void method2()
   {
               System.out.println("implementation of method2");
   }
   public static void main(String arg[])
   {
               MyInterface obj = new Demo();
               obj.method1();
   }
}
This is how a class implements an interface. It must provide the body of all the methods that are declared in interface or in other words you can say that class has to implement all the methods of interface.



Output:

implementation of method1

Key points

Here are the key points to remember about interfaces:
1) We can’t instantiate an interface in java. That means we cannot create the object of an interface
2) Interface provides full abstraction as none of its methods have body. On the other hand, abstract class provides partial abstraction as it can have abstract and concrete (methods with body) methods both.
3) implements keyword is used by classes to implement an interface.
4) While providing implementation in class of any method of an interface, it needs to be mentioned as public.
5) Class that implements any interface must implement all the methods of that interface; else the class should be declared abstract.
6) Interface cannot be declared as private, protected or transient.
7) All the interface methods are by default abstract and public.
8) Variables declared in interface are public, static and final by default.
9) Interface variables must be initialized at the time of declaration otherwise compiler will throw an error.



interface interfaceExample
{
      int x;//Compile-time error
}

10) An interface can extend any interface but cannot implement it. Class implements interface and interface extends interface.


11) A class can implement any number of interfaces.

No comments:

Post a Comment

If any suggestions or issue, please provide