Interfaces in Java

Interface is used to provide full abstraction in java. It also represents IS-A relationship. Interfaces are declared using the keyword interface.

Quick point: IS-A is subsumption relationship between attractions.
  1. A method it is declared inside the interface can be added with public and abstract by compiler.
  2. A variable it is declared inside the interface can be added with public, static and final by compiler.
Note:
One class extends another class(s) and one interface extends another interface(s) but a class implements interface(s).

Ex:
package first;
interface myinter {
    void input();
}
class first implements myinter {
    public void input() {
        System.out.println("Hello interface");
    }
    public static void main(String a[]) {
        first obj = new first();
        obj.input();
    }
}
Output:
>javac myinter.java
>java A
Hello interface

Multiple inheritance in java is only possible by interfaces:

1. Class implements multiple interfaces
2. Interface extends multiple interfaces

Multiple inheritance is  not possible through the classes but supported with interfaces:
Ex:
interface inter {
    void first();
}
interface inter1 {
    void first();
}
class myclass implements inter, inter1 {
    public void first() {
        System.out.println("Multiple Inheritance");
    }
    public static void main(String args[]) {
        myclass my = new myclass();
        my.first();
    }
}
Note: Save the above program with the name as inter.java
>javac inter.java
>java myclass
Multiple Inheritance 

Interface inheritance:
One Interface extends another interface
interface inter{
void first();
}
interface inter1 extends inter{
void second();
}
class myclass implements inter1{
public void first(){System.out.println("Multiple Inheritance");}
public void second(){System.out.println("interface inheritance");}
public static void main(String args[]){ 
myclass my= new myclass();
my.first();
my.second();
}
}
Output:
>javac inter.java
>java myclass
Multiple Inheritance
interface inheritance

Extending interface occurs when parent interface doesn't contain any methods.

Marker interface or tagged interface:

An interface with no member is called marker inteface or tagged interface.
Ex: Cloneable, Serializable, Remote etc..

Design purposes of tagging interface:

Nested interface:
An interface declared inside another interface or class is called nested interface.

i. Nested interface can only be accessed by the outer interface or class and cannot be accessed directly.
ii. Nested interface must be public and are static implicitly.
iii. A class can be declared under interface called nested class. It is automatically converted as public static nested class by java compiler

Uses of  interfaces:
Provides 100% abstraction.
Multiple inheritance possible


Key points:
i. we cannot instantiate the interface in java
ii. While providing implementation in class of any method of an interface, it needs to be mentioned as public
iii. Class implementing interface must implement all the methods of interface otherwise class should be declared as abstract.
--When a class is defined to implement an interface, the class must provide definitions of all the methods defined in the interface. Otherwise, you will get compiler time error.
Why: The point of interface is to guarantee that object will have to behave as interface specifies it will.
But: (Exceptional case)
interface inter {
    public void method();
}
class myclass {
    public void method() {
        System.out
                .println("This class is not implementing the 'inter' interface");
    }
}
class myclass1 extends myclass implements inter {
}//implementation class mainMethod {
    public static void main(String a[]) {
        new myclass1().method();
    }
}
>javac inter.java
>java mainMethod
This class is not implementing the 'inter' interface

iv. Interface cannot be declared as private, protected and transient
v.  All the interface methods are by default abstract and public.
vi. Variables declared in interface are public, static and final by default.
 vii. Interface variables must be initialized otherwise compiler shows error
viii. We cannot change the value of variable in the implementation class of any interface.
ix.A class cannot implement the methods with different return type but same name in two different interfaces
x. An interface doesn't contain any constructors
xi. Interface implicitly Abstract

No comments:

Post a Comment