Interface in Java programming language

Interface in java programming language

In this tutorial, we will discuss the Interface in Java programming language
 

An interface in Java similar a class but it is not a class. It is the blueprint of a class. An interface can contain any number of data-members and methods with an empty implementation.

A Java interface is just a collection of abstract methods interface instead of multiple inheritance.

All the methods of interface are abstract and should be overridden in subclass.

Why interfaces are needed in Java?

No multiple inheritance in Java – Cannot extend to more than one class at a time. Interfaces use Java instead of multiple inheritance  because multiple inheritance is not supported in Java.

An interface can extend to another interface similar to the way that a class can extend to another class.

A class can implement more than one interface at a time.

 

The relationship between classes and interfaces

 

interface in java
As shown in the diagram above, a class extends to another class, an interface extends to another interface but a class implements an interface.

How to declare a interface

The interface is declared by using interface Java keyword. all the methods of interface declared with the empty body it is called abstraction. When the class is implemented by using interface must implement all the methods declared inside the interface
Syntax
interface <interface_ame>{
//declare variables;
//declare metods that abstract;
}
Example of interface
public interface  calc
{
//data member
int age ;
float salary;
double total;
boolean isEmpty
//methods
public void calculate ();
public void total(float f1, float f2);
}

Example programs

program 1

interface welcome  // interface named welcome
{
void print();
}
class Class implements welcome{  //class named Class
public void print()
{
System.out.println(“welcome to java”);
}
public static void main (String args[]){  //main method in java
Class A=new Class();// object for class Class
A.print();
}

}

Example

Program 2

interface office{
void display();
void print();
}
class staffs implements office
{
public void display()
{
System.out.println(“this is my display”);
}
public void print()
{
System.out.println(“this is my print”);
}
public static void main(String args[]){
staff obj=new staff();
obj.display();
obj.print();
}
}

Example

Program 3

interface shape //interface
{
double pi=3.14;
void calculate(float f1, float f2);
}
class circle implements shape  // class circle implement using interface
{
public void calculate(float x, float y)
{
System.out.println(“Area of circle:”+(pi*x*y));
}
}
class regrangle implements shape  // class regtangle implement using interface
{
public void calculate(float x, float y)
{
System.out.println(“Area of regtangle:”+(x*y));
}
}
class multipleinherit
{
public static void main(String args[])
{
shape s1; // create reference for interface
circle c1=new circle(); //create object for circle class
regrangle r1=new regrangle(); //create object for regtangle class
s1=c1; //assign object c1 to interface
s1.calculate(10,6); //called method with argument
s1=r1;
s1.calculate(15,6); //called method with argument
}
}

Example
Related Links    

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.