Default and Static Methods in Interfaces

Default and Static Methods in Interfaces

In the Java programming language, prior to Java 8, interfaces could mostly contain user-defined custom abstract methods. Java 8 introduced several default and static methods, allowing Java users to declare or include a custom method implementation within an interface.

Default and Static Methods in Interfaces

Default Method in Java Interface.

A default method in a Java program is a custom method declared within a class interface that has a user-defined body/implementation declaration.

A Java user can declare or define a default method by using the default keyword in a class.

Syntax of Default Method.

interface Student {

default void course() {

System.out.println(“Select your desired course”);

}

}

In this example, a class implementing the interface can directly use the default method.

Example of Default Method Interface.

interface Student {

default void course() {

System.out.println(“Student can select nielit CCC course”);

}

}

class Ccc implements Student {

}

public class Main {

public static void main(String[] args) {

Ccc c = new Ccc();

c.course();

}

}

Its output.

Student can select nielit CCC course

In this example, the Ccc class does not need to implement course() because the interface already provides a default implementation.

Overriding a Default Method in a Java Program.

A class implementing a default method in a Java program can override the default method.

interface Student {

default void course() {

System.out.println(“Student can select their desired course”);

}

}

class Olevel implements Student {

@Override

public void course() {

System.out.println(“Student can select Nielit O Level course”);

}

}

public class Main {

public static void main(String[] args) {

Olevel olevel = new Olevel();

olevel.course();

// Here it is using the interface class reference

Student student = new Olevel();

student.course();

}

}

Its output.

Student can select Nielit O Level course

In this example, the user-declared class default method provides a default implementation, but a user-defined class implementing it can replace it.

Why were default methods introduced in Java?

One of the key reasons for introducing default method implementations in the Java language was to allow class interfaces to evolve without breaking existing default class method implementations.

Suppose this class method is originally defined in an interface within a class.

interface Employee {

void join();

}

Here, this class method may already be implemented by multiple classes.

class Company implements Employee {

@Override

public void join() {

System.out.println(“Employee join company”);

}

}

If a new abstract method is added to this class method.

void leave();

then every existing implementing class would have to implement it.

Instead, Java can provide its users with a default implementation.

interface Employee {

void join();

default void leave() {

System.out.println(“Employee leaves company”);

}

}

Existing classes can continue to function without implementing the leave() function.

Complete example of default methods.

interface Employee {

void join();

default void leave() {

System.out.println(“Employee leaves company”);

}

}

class Company implements Employee {

@Override

public void join() {

System.out.println(“Employee joins company”);

}

}

public class Main {

public static void main(String[] args) {

Company company = new Company();

company.join();

company.leave();

}

}

Static method concept in Java interfaces.

In a Java program, a static class method is connected to the interface itself, not to any object implementing the class interface.

Java users can declare and define it by using the static keyword in their program.

Example of the static method concept.

interface Total_value {

static int total(int p, int q) {

return p + q;

}

}

//Here, the method is called using the interface name.

public class Main {

public static void main(String[] args) {

int add_value = Total_value.total(4, 7);

System.out.println(add_value);

}

}

Its output.

11

Static methods in Java cannot be called from an object.

This is false.

Total_value total = …;

t.total(4, 7); //

// This is the correct way.

total_value.total(4, 7); //

Here, the static interface methods are the same as those of the interface.

Static methods cannot be overridden in a Java program.

Static methods overridden example.

interface Sample {

static void preview() {

System.out.println(“Sample class Interface”);

}

}

// Here, in this example, a class can have a method with the same name.

class Presentation implements Sample {

static void preview() {

System.out.println(“Presentation class interface”);

}

}

public class Main {

public static void main(String[] args) {

Sample.preview();

Presentation.preview();

}

}

But in this, it is not overriding the static method of the interface. They can be different static methods.

Main difference between default vs static methods

#Default interface class methodStatic interface class method
1.Uses default keyword for default method interface declarationUses static keyword for static class method interface declaration
2.Default interfaceclasshas its own a method bodyStatic interface classhas a its own method body
3.Default interfaceclasscan be overriddenStatic interface classcannot be overridden
4.Default interface class methodcalled using an implementing objectStatic interface classmethodcalled using interface name
5.Default interface class methodsupports polymorphism methodStatic interface classmethoddoes not participate in instance polymorphism method
6.Default interface class method firstintroduced in java 8Static interface classmethodfirstintroduced in java 8

Example of default vs. static methods.

interface Sample {

default void show() {

System.out.println(“Default class method”);

}

static void preview() {

System.out.println(“Static class method”);

}

}

public class Main {

public static void main(String[] args) {

Sample obj = new Sample() {};

obj.show(); // Here we define the default class method

Sample.preview(); // Here we define the static class method

}

}

Multiple class interfaces in Java with the same default method.

In a Java program, a user-defined class can implement or implement multiple interfaces simultaneously.

Let’s assume that in this program.

interface Sample1 {

default void preview() {

System.out.println(“Sample1 class element”);

}

}

After this.

interface Sample2 {

default void preview() {

System.out.println(“Sample2 class element”);

}

}

Now.

class Group implements Sample1, Sample2 {

}

This can lead to a class interface conflict, as Java doesn’t know which preview() function method to use.

In this case, the class must override it.

class Group implements Sample1, Sample2 {

@Override

public void preview() {

System.out.println(“Group”);

}

}

Calling the default method of a specific interface in Java.

In a Java interface program, Java users can call the default methods of a specific class interface in a clear order, within the class implementing the class.

InterfaceName.super.methodName();

Calling the default method of an instance of a specific interface.

interface ClassFirst {

default void preview() {

System.out.println(“Class First”);

}

}

interface ClassSecond {

default void preview() {

System.out.println(“Class Second”);

}

}

class SampleClass implements ClassFirst, ClassSecond {

@Override

public void preview() {

ClassFirst.super.preview();

ClassSecond.super.preview();

System.out.println(“Sample Class”);

}

}

public class Main {

public static void main(String[] args) {

SampleClass obj = new SampleClass();

obj.preview();

}

}

Its output.

class first

Class Second

Sample Class

Important points about Default and Static Methods in Interfaces.

Default Method

default void preview() {

// User-defined class interface implementation

}

  • First introduced in Java 8.
  • It is the body of a class interface.
  • It can be overridden within a class interface.
  • It is called by a class object that implements the class.
  • It helps add new functionality to an interface while maintaining compatibility across class interfaces.

Static Method

static void preview() {

// User-defined class interface implementation

}

  • First introduced in Java 8.
  • It is the body of a class interface.
  • Static methods cannot be overridden within a class.
  • Static methods are called using the interface name.
  • Useful for utility/helper methods associated with interfaces in Java programs.

Easy distinction between Java Default and Static Methods.

Default Class Method

↓

Object → obj.method()

Static Class Method

↓

Interface → Interface.method()

Example of both Default and Static Methods.

interface Sample {

default void display() {

System.out.println(“Default method defined”);

}

static void preview() {

System.out.println(“Static method defined”);

}

}

public class Main {

public static void main(String[] args) {

Sample obj = new Sample() {};

obj.display(); // Default class method defined

Sample.preview(); // Static class method defined

}

}

Leave a Reply