Interface by G Krishna Chauhan

An interface in java is a blueprint of a class. It has static constants and abstract methods only.

The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.

Use of Java interface:
  • It is used to achieve fully abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.


Keyword For inherit the abstract Method : implements



Example Program of Interface :


interface a
{
public void sum();
}
interface b
{
public void sub();
}
class xy implements a,b 
{
public void sum()
{
System.out.println("i am sum");
}
public void sub()
{
System.out.println("i am sub");
}
}
abstract class xyz1 extends xy
{
abstract public void mul();
}
class xyz extends xyz1
{
public void mul()
{
System.out.println("i am mal");
}
}
class test
{
public static void main (String args[])
{
xyz x=new xyz();
x.sum();
x.sub();
x.mul();
}
}

No comments:

Post a Comment