Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Interface in Java programming language

Posted on February 4, 2017January 29, 2020

Table of Contents

  • Interface in java programming language
    • How to declare a interface
    • Related

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 programming language
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();
}

}

Interface in Java programming language
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();
}
}

Interface in Java programming language
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
}
}

Interface in Java programming language
Example
 
Related Links          
 
Single level inheritance in java         Single level inheritance in C++
Multi levelinheritance in Java         Multilevel inheritance in C++
 
Hierarchical Inheritance in Java     Hybrid inheritance in C++
Inheritance in Java             Inheritance in C++          Multiple Inheritance in C++

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes