An interface s similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Java Interface also represents the IS-A relationship.It cannot be instantiated just like the abstract class. Since Java 8, we can have default and static methods in an interface. Since Java 9, we can have private methods in an interface.
Why use Java interface
-
It is used to achieve
abstraction.
-
By interface, we can
support the functionality of multiple inheritance.
-
It can be used to
achieve loose coupling.
How Diffrent an It can be used to achieve loose coupling:
-
You cannot instantiate
an interface.
-
An interface does not
contain any constructors.
-
All of the methods in
an interface are abstract.
-
An interface cannot
contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
-
An interface is not
extended by a class; it is implemented by a class.
-
An interface can
extend multiple interfaces.
Declaring Interfaces
The “interface” keyword is used to declare an interface. Here is a simple example to declare an interface −
Example
Following is an example of an interface −/* File name : NameOfInterface.java */ import java.lang.*; // Any number of import statements public interface <interface_name> { // Any number of final, static fields // Any number of abstract method declarations // By default }
Properties
-
An interface is
implicitly abstract. You do not need to use the abstract keyword
while declaring an interface.
-
Each method in an
interface is also implicitly abstract, so the abstract keyword is
not needed.
-
Methods in an
interface are implicitly public.
Implementing Java Interfaces
interface printable{ void print(); } class A6 implements printable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } }When overriding methods defined in interfaces, there are several rules to be followed −
-
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.
-
The signature of the
interface method and the same return type or subtype should be
maintained when overriding the methods.
-
An implementation
class itself can be abstract and if so, interface methods need not
be implemented.
-
An implementation
class itself can be abstract and if so, interface methods need not
be implemented.
When implementation interfaces, there are several rules −
-
A class can implement
more than one interface at a time
-
A class can extend
only one class, but implement many interfaces.
-
An interface can
extend another interface, in a similar way as a class can extend
another class.
0 comments:
Post a Comment