Interfaces in Java
In Java programming, a class interface is a reference-type method of a class, defined similarly to a user-defined class. In a class interface, only constants and abstract methods of the class can be defined or declared. The interface concept in Java is used to indicate what a declared class can do and how it can perform in an existing program. The role of a class interface is to indicate how a class will function within a class. It implements its interface. In Java, providing custom behavior method implementations for methods declared within custom interfaces is crucial.

In Java programming, an interface is an advanced implementation method. Class interfaces are used to achieve many user-defined custom purposes. For example, declaring abstraction, multiple inheritance, and polymorphism within a class can address concepts such as polymorphism.
Defining an Interface in a Java Class.
In a Java class, an interface is declared using the interface keyword. Interface methods are defined in a class without a body, so that they must be implemented by any class implementing the interface.
Syntax for declaring an interface in a Java class.
interface Course {
/ here we define an abstract class method with no body
void select();
/ here we define a Constants implicitly public, static, and final
int p = 7; // here we declare a constant data type variable
}
Declaring an interface in a Java class example.
- In the interface syntax here, select() is an abstract class method defined in the interface, and its body is not declared or defined in the interface.
- Similarly, the int p variable is a constant data type variable defined in the interface.
Implementing an interface in a Java class.
In a user-defined Java class program that implements an interface, the class must provide implementations for all class methods declared in the interface. Java users can define this by using the implements keyword.
Syntax for implementing an interface in Java.
class Java implements Course {
// Here we need to provide the implementation for the abstract class method from the class interface.
@Override
public void select() {
System.out.println(“Java select”);
}
}
Implementing an interface example.
- Here, in this class interface, the Java class implements an interface of the Course class, and it provides an implementation for the select() class function method.
Using an interface in a Java class.
Once a Java user implements a class interface, they can create multiple objects of that class by invoking the class methods defined in the interface. Here, the class object declared in the current class behaves according to the implementation provided in the class.
Example of using an interface in a Java class.
interface Course {
void select_course(); // here we declare an abstract class method
int price = 999; // here we define a price Constant data type
}
class Java implements Course {
@Override
public void select_course() {
System.out.println(“Java select”);
}
}
class Python implements Course {
@Override
public void select_course() {
System.out.println(“Python select”);
}
}
public class Main {
public static void main(String[] args) {
Course java = new Java(); // Here we are creating an object of the Java class
Course python = new Python(); // Here we are creating an object of the Python class
java.select_course(); // Here it calls the Java select_course() method
python.select_course(); // Here it calls the Python select_course() method
System.out.println(“Course’s price is – ” + Course.price); // Here it accesses a constant from the class interface
}
}
Explanation of using an interface in a Java class.
- In the Java class program here, the Java and Python classes implement the Course superclass interface and provide their own implementations of the select_course() method. Both the Java and Python classes are referenced by the Course type variable. This indicates in the Java class how an interface allows polymorphic behavior. As such, this user-defined method call of the same class behaves differently depending on the object’s data type.
- The constant data type price from the class interface is accessed via Course.price. As an interface constant in a class can be of public, static, and final data type nature by default.
Features of a Java Class Interface.
Methods in a Class Interface.
By default, all methods in an interface defined in a Java class program are abstract in nature, except for static and default methods available in Java version 8.
Java users cannot provide a method body in a class interface unless they use the default or static keyword in Java version 8 and later.
Java class methods are implicitly public and abstract in nature unless they are declared or defined as default or static.
Constants in a Class Interface.
All variables declared in a Java class interface are implicitly public, static, and final in nature. Because of this, they must be initialized when declaring them in a class program.
Access Modifiers in a Class Interface.
Java class interface methods are always public in nature, even if the Java user does not specify them with a public access modifier.
Variables are also defined as public, static, and final in nature.
Multiple Class Inheritance.
In contrast to class interface programming, where class inheritance inherits from only one superclass’s properties, a class in a Java class programming can provide multiple interface implementations, allowing it to achieve the multiple inheritance of class behavior.
Java Class Interface Inheritance.
In the Java class inheritance concept, one class interface can extend another interface, allowing the class to develop a hierarchical interface structure.
Default methods in a Java class interface/in Java 8 and above.
In Java version 8, class interfaces can define default methods, which are defined as methods with the existing class body. Default class methods in Java version 8 provide features to add new class methods to the Java user interface in a unique order without breaking existing classes implementing it.
Syntax of a default method in a Java class.
interface Course {
/ Here we define an abstract class method
void select_course();
/ Here we use the default class method with body
default void selection() {
System.out.println(“Java course selection”);
}
}
class Java implements Course {
@Override
public void select_course() {
System.out.println(“Java select”);
}
}
public class Main {
public static void main(String[] args) {
Java java = new Java();
java.select_course(); // Here it calls the Java select_course() method
java.selection(); // here it calls the default selection() method from the Course interface
}
}
Explaining the default method in a Java class.
- Here, the select_course() method is a default class method defined in the Course class interface. It has a body defined, and is available for all implementing classes to use, unless they manually override it.
- Here, the Java class implements the selection() function and uses the default implementation of select_course().
