In this article, you will learn What is an Interface in Java Programming Language and how to use it in a java program. Basically, an interface is a point where two systems meet and interact. For Example, the touchscreen on your smartphone is an interface between you and all the device functionality.
In Java Programming Language, an interface is just like a class. It contains abstract methods and static constants. However, static and default methods are included in the newest version of java i.e, Java 8.
Like classes, interfaces also saved in a .java file extension and may contain as many abstract, static and default methods as classes do.
Also Read:– Packages in Java Programming Language
Now before going onto the detailed description on interfaces lets first check out the differences between an interface and a class.
DIFFERENCE BETWEEN INTERFACES AND CLASSES
- A class cannot extend an interface. It can implement an interface instead.
- An interface cannot be instantiated.
- Constructors are absent in interfaces.
- Instance variables are absent in interfaces. It only contains variables that are declared either final or static.
- Unlike classes, interfaces do not use the keywords ‘abstract’ and ‘public’ to define an abstract method and access modifiers respectively. They inherently possess the properties of both the ‘abstract’ and ‘public’ keywords.
- Unlike classes, interfaces supports multiple inheritance.
INTERFACE DECLARATION AND IMPLEMENTATION
In java programming language, an interface is declared by using the keyword ‘interface’. We have shown below the syntax and how an interface is implemented in a java program.
Syntax:
public interface InterfaceName {
// final and static variables
// default, static and abstract methods
}
Example 1: Class implementing an Interface
interface W3TrainingSchool {
public void courseA( ) ;
public void courseB( ) ;
}
class W3TS implements W3TrainingSchool {
public void courseA( ) {
System.out.println(“Digital Marketing”) ;
}
public void courseB( ) {
System.out.println(“Web Designing”) ;
}
public static void main(String []args) {
W3TS obj = new W3TS( ) ;
obj.courseA( ) ;
obj.courseB( ) ;
}
}
Output:
Digital Marketing
Web Designing
Example 2: An Interface extending another Interface
interface W3TrainingSchool {
default public int courseFee ( ) {
return(0) ;
}
}
interface DigitalMarketing extends W3TrainingSchool {
default public int courseFeeDM ( ) {
return(50000) ;
}
}
interface PHP extends W3TrainingSchool {
default public int courseFeePHP ( ) {
return(30000) ;
}
}
interface WebDesigning extends W3TrainingSchool {
default public int courseFeeWD ( ) {
return(45000) ;
}
}
class ExtendingInterface implements DigitalMarketing, PHP, WebDesigning {
public static void main(String args[]) {
ExtendingInterface obj1 = new ExtendingInterface ( ) ;
System.out.println(“Fee of Digital Marketing =” + obj1.courseFeeDM ( )) ;
ExtendingInterface obj2 = new ExtendingInterface ( ) ;
System.out.println(“Fee of PHP =” + obj2.courseFeePHP ( )) ;
ExtendingInterface obj3 = new ExtendingInterface ( ) ;
System.out.println(“Fee of Web Designing =” + obj3.courseFeeWD ( )) ;
}
}
Output:
Fee of Digital Marketing = 50000
Fee of PHP = 30000
Fee of Web Designing = 45000
INTERFACE SUPPORTS MULTIPLE INHERITANCE
A class in java does not support multiple inheritance. However, interfaces allow us to use multiple inheritance. This phenomenon involves extending or inheriting one or more interfaces by another interface.
Let’s make this concept more clear to you through an example. The basic syntax is shown below.
Syntax:
public interface InterfaceName3 extends InterfaceName1, InterfaceName2 { }
Example:
interface JAVA {
void courseA( ) ;
}
interface PHP {
void courseB( ) ;
}
class W3TrainingSchool implements JAVA, PHP {
public void courseA( ) {
System.out.println(“W3Training School offers Java Course”) ;
}
public void courseB( ) {
System.out.println(“W3Training School offers PHP Course”) ;
}
public static void main(String args[]) {
W3TrainingSchool obj = new W3TrainingSchool ( ) ;
obj.courseA( ) ;
obj.courseB( ) ;
}
}
Output:
W3Training School offers Java Course
W3Training School offers PHP Course
Also Read : String Classes in Java Programming
We have provided you the best possible description on Interfaces in Java Programming Language and How they differ with classes. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.