• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Interface


Interface is similar to class.It is used to achieve the multiple inheritance in java language.
Interface is replacement of abstract class.
Syntax:
interface InterfaceName
{
list of public static final variables;
list of abstract methods();
}

Every interface can be identify with interface keyword and represented by user defined name.

Defination of interface:
Interface is similar to class,Which is collection of public static final variables and abstract methods.
The main advantege with interface is common reusability that means these methods can be reused many number of classes with separate implementation part.
Interface always contains variable initilizations and abstract methods as shown below.

interface Demo
{
int x=10; -> the signature is public static final int x=10;
void show(); -> abstract void show();
void disp();-> abstract void disp();
}

The properties of interface can be reuse any class with the help of implements keyword.

class classname implements InterfaceName1,InterfaceName,...
{
..
..
}

Every abstract method of interface should be overridden in the implemented class otherwise that class becomes abstract class.
Note:
If any abstract method is not overridden the derived class then that derived class also becomes abstract class(every abstract method should be overridden).
The main disadvantage in abstract class is it does not support multiple inheritance,so to overcome this problem we use interface.

Key Points

Image
Interface Program

interface Addition
{
void add();
}
interface Substraction
{
void sub();
}
class Demo implements Addition,Substraction
{
int x,y,z;
public void add()
{
x=100;
y=200;
z=x+y;
System.out.println(z);
}
public void sub()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}
void mul()
{
x=100;
y=200;
z=x*y;
System.out.println(z);
}
}
class Main
{
public static void main(String args[])
{
Demo d=new Demo();
d.add();
d.sub();
d.mul();
}
}


Output: