Introduction to Java Interface - How to make a Java Interface

11:17 AM 0 Comments

           

Interface & Java interface example 



     Java interface tutorial will teach you how to grab the concept of Java’s interface. Interface is an electrifying feature of Java Programming Language. It helps you make meaningful code. We use a special keyword for defining an interface called 'interface'. To implement an interface, a class must create the complete set of methods defined by the interface. Java supports one interface and multiple methods, this feature achieves polymorphism.



Why do we need an interface?

Are you confused with interface?. Don’t panic,  Interfaces are syntactically similar to classes; expect the instance variable and method definition. Interface does not have instance variables and methods are declared without body. Once an interface is defined, any number of classes can implement an interface. To implement interface on your program, a class must create the complete set of method defined by the interface. Java allows you to fully utilize the "one interface " and "multiple methods" aspect of polymorphism.

Defining an Interface

access interface-name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
return-type method-nameN(parameter-list);
type final variable=value;
}

Implementing Interface

class classname [extends superclass]
[implements interface[,interface..]]
{
class body
}


Java Interface Tutorial : Simple and Fruitful Java Interface Example

Create an interface and save it as a single file with .java extension (here company.java).

    
interface company
{
public void info();
public void show();
public final double tax=20.89;
}

Create a new java file that uses the company interface.










 
import java.io.*;
import java.lang.System;
class employee implements company
{
int tax_now;
double tax_rate;
employee(int a,double b)
{
tax_now=a;
tax_rate=b;
}
public void info()
{
double rate;
java.util.Date date=new java.util.Date();
rate=tax*tax_now*tax_rate;
System.out.println("Tax calculated as :"
+"("+date+")"+rate);
}
public void show()
{
System.out.println("thank you for using tax app.");
}
}
public class office
{
public static void main(String[] args)
throws IOException
{
BufferedReader b;
b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the current tax rate");
int t_rate=Integer.parseInt(b.readLine());
System.out.println("enter the coming tax");
double tr=Double.parseDouble(b.readLine());
employee obj=new employee(t_rate,tr);
obj.info();
obj.show();
}
}

Accessing implements through interface reference

You can declare variables object reference that use an interface rather than a class type. Any instance of any class that implements the declared interface can be referred to by such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface of the interface being referred to. The following code will help you to understand the logic.
class InterTest
{
public static void main(String[] args)
{
Call c=new CallBack();
c.back(1000);
}
}

Nested Interfaces
An interface can be declared a member of a class or another interface. Such an interface is called member interface or nested interface. A nested interface can be declared as public, private or protected.

Hi Nikinmon SK i like to share my knowledge !