Oop Java

Interface in Java programming language

interface in Java programming language

In this tutorial, we will learn  about the interface in Java programming language

An interface in Java is similar to a class but it is not a class.

interface keyword is used to create an interface in Java.

An interface is an OOP concept and a technique to achieve abstraction.

The interface can contain the only abstract method with No declaration

 

An interface can have any number of data types and methods including empty implementation.- abstract method

A Java interface is just a collection of abstract methods. interface introduces instead of multiple inheritance to Java programming language.

If all the methods inside the interface are abstract, it should be overridden in the subclass.

 

Are interfaces needed in Java?

It is used to reach abstraction(another way is the abstract class)

multiple inheritance are absent in Java – Java does not allow inheritance to more than one class at a time.

In the Java language, multiple inheritance is avoided for security purposes.

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

the implement is a keyword that is used to implement methods or data.

 

The relationship between class and interface

Here, We can observe the relationship between class and interface in Java

the relationship between classes and interfaces

We can use extends keyword to relates between two interfaces.

We can use extends keyword to relates between two classes.

 

The relation between interface and class

But we can uses implements keyword to relate between interface and class.

Declaration

Syntax of interface

public interface NameOfInterface{
//anynumberof fields
//anynumberof abstract method declaration
}

For example

public interface calc{
//fields
int age;
float average;
double salary;
Boolean isempty;
//method declaration
public void calculate();
public void total(float f1,float f2);
 }

Example

Program 1

interface MyInterface{
void print();
}
class Welcome1 implements MyInterface{
public void print()
{
System.out.print("welcome to my java webpage");
}
public static void main (String args[]){

Welcome1 obj=new Welcome1();
obj.print();
}

}

 

When the above code executed, it will produce the following output

welcome to my Java webpage

In the above example, the MyInterface is an interface has void print() method(only one) with no implementation. but its implementation provided in the welcome1 class

Program 2

interface myschool{
public void print();
public void display();
} 

class teachers implements myschool{
public void print(){
System.out.println("This is my bio data");
}

 public void display(){
System.out.println("This is my result");
} 
public static void main (String args[]){
teachers obj=new teachers();
obj.print();
obj.display();
}


}

In the above example, the MySchool is an interface has void print(), void display methods(two) with no implementation. but its implementation provided in the teachers class

 

When the above code is executed, it will produce the following output

This is my bio data
This is my result

 

Program 3

interface shape
{
double pi=3.14;
void calculate(float f1,float f2);
}

class circle implements shape{
public void calculate(float a,float b){
System.out.println("Area of circle :"+(pi*a*b));
}
}
class regtangle implements shape{
public void calculate(float a, float b){
System.out.println("Area of regtangle :"+(a*b));
}
}
class interface1{
public static void main(String args[]){
shape s1;
circle c1=new circle();
regtangle r1=new regtangle();
s1=c1;
s1.calculate(10,6);
s1=r1;
s1.calculate(12,8);
}

}

When the above code is executed, it will produce the following output

Area of circle :188.4
Area of rectangle :96.0

 

This is the program used to calculate the area of some shapes. This is implemented from the interface shape.

 

There are other Java language keywords that are similar to this post

extends keyword in Java

implements keyword in Java

interface keyword in Java

Abstract keyword in Java

class keyword in Java

public keyword in Java

 

Suggested for you

method in Java language

Encapsulation  in Java language

Abstract class in Java languag

Method overloading in Java language

Constructor in Java Language

Method overriding in Java language

Constructor overloading in Java language

difference to constructor and method in Java

Encapsulation in Java language

 

 

Abstract class in Java programming language
Method in Java programming language with example
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago