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
We can use extends keyword to relates between two interfaces.
We can use extends keyword to relates between two classes.
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
Suggested for you
EncapsulationĀ in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language