Interface in Java

 Interface in Java

We discussed the Abstract class in the last tutorial, which is used to provide partial abstraction. For full abstraction, an interface is used instead of an abstract class. Abstraction is the process of showing just "useful" facts to the user while hiding unnecessary details.

Interface in Java
Interface in Java

Interface looks like a class but it is not a class. An interface, like a class, can have methods and variables, however interface methods are abstract by default (only method signature, no body). Variables defined in an interface are also by default public, static, and final.

syntax :

interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}


Example :

interface test 
{
void show();
}
class Demo implements test
{
public void show()
{
System.out.println("Hello");
}

public static void main(String args[])
{
Demo obj = new Demo();
obj.show();
}
}

Output :
Hello


Why use Java interface?

There are three primary reasons to use a interface. Below is a list of them.

  • It is used to achieve abstraction
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.
rule for interface
Rules for interface

Here are the imp points to remember about interfaces:

  • We can’t instantiate an interface in java
  • Interface provides full abstraction as none of its methods have body.
  • implements keyword is used by classes to implement an interface.
  • While providing implementation in class of any method of an interface, it needs to be mentioned as public.
  • Class that implements any interface must implement all the methods of that interface, else the class should be declared abstract.
  •  Interface cannot be declared as private, protected or transient.
  • All the interface methods are by default abstract and public.
  • Variables declared in interface are public, static and final by default.
  • Interface variables must be initialized at the time of declaration otherwise compiler will throw an error.
  • Inside any implementation class, you cannot change the variables declared in interface because by default, they are public, static and final.
  • An interface can extend any interface but cannot implement it. Class implements interface and interface extends interface
  • A class can implement any number of interfaces.

Nested Interface :

An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface.

Points to remember for nested interfaces :
  • The nested interface must be public if it is declared inside the interface, but it can have any access modifier if declared within the class
  • Nested interfaces are declared static.
Example ;
interface Test1 
{
void show();
interface Message
{
void msg();
}
}
class Nestedinterface implements Test1.Message
{
public void msg()
{
System.out.println("Hello nested interface");
}

public static void main(String args[])
{
Test1.Message obj = new Nestedinterface(); //upcasting here
obj.msg();
}
}

Output :
Hello nested interface


Classes and interface Relationship :

classes and interface relationship
classes and interface relationship

in above image, A class extends another class, an interface extends another interface, but a class implements an interface,

Advantages of Interface:
  • Without bothering about the implementation part, we can achieve the security of implementation
  • Multiple inheritance is not allowed in Java, but you can use interfaces to do so since you can implement multiple interfaces.
Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post