Questions tagged [default-method]

A default method is a feature introduced in Java 8 which allows an interface to declare a method body. Classes which implement the interface are not required to override a default method. Use this tag for questions relating to default methods.

A default method is a feature introduced in which allows an interface to declare a method body. Classes which implement the interface are not required to override a default method. An interface method is made default by adding the default keyword, also introduced in Java 8.

In the following example, then method is a default method of the Command interface.

@FunctionalInterface
interface Command {

    void execute();

    default Command then(Command next) {
        return () -> {
            this.execute();
            next.execute();
        };
    }

}

Adding a default method to an interface or changing a method from abstract to default does not break compatibility with a pre-existing binary if the binary does not attempt to invoke the method.

See also:

168 questions
2
votes
0 answers

Interface: An Edge over Abstract Class with Defualt Method (Just A Discussion)

With the official release of Java 8, every developers specially the java geek were very much interested for Lambda Expression. And they have the reasons to excited since scala and clojure had already made a drastic impact over the development on…
Nasruddin
  • 1,640
  • 14
  • 20
1
vote
1 answer

Override Object methods inside interface using default methods- Misleading error

Question: Need to understand why in case of Program 1 getting this misleading error, but in Program 2 it gives correct error? Also why unlike toString(), equals(Object), and hashCode(), overriding clone() and finalize() inside interface is…
1
vote
0 answers

VerifyError on Kitkat when code explicitly calls a default method

When instantiating a class that has code to explicitly call a default method for an interface that it implements, a java.lang.VerifyError is thrown. This happens on Kitkat (API 19), but Lollipop and more recent versions work fine. The default method…
1
vote
1 answer

Class inheriting same method from two different interfaces and a single default implementation won't compile

I have stumbled upon a strange issue working with default methods. Consider this situation: interface A { int method(); } interface B { default int method() { return 1; } } class C implements A, B { void anotherMethod() {…
gscaparrotti
  • 663
  • 5
  • 21
1
vote
1 answer

OpenAPI generator returns 501 for implemented method

I've generated rest api with openAPI generator maven plugin and I've overridden the default method from MyApiDelegate interface, but POST request on /endpoint provides 501 NOT IMPLEMENTED as if I hadn't given my own implementation of that method in…
obolen_an
  • 41
  • 6
1
vote
1 answer

Override DefaultGroovyMethods method

Is there a way to easily override toSet(Collection self) method from DefaultGroovyMethods? Its implementation uses HashMap public static Set toSet(Collection self) { Set answer = new HashSet(self.size()); …
ilPittiz
  • 734
  • 1
  • 10
  • 23
1
vote
1 answer

How to use default interface implementation with kotlin Multiplatform and Swift

I'm using KMP to make a crossplatform app both on Android and iOS. My problem is that when I create an interface in Kotlin common main with a default implementation, I can't use that implementation in iOS and I need to rewrite the code in Xcode.…
1
vote
1 answer

Type-variable-dependent default method implementation in Haskell

I am trying to define default method implementations, but only if the class's type variables derive certain other classes. I have tried creating type-dependent instances using => (am I even using it correctly?), but I get a "duplicate instance…
1
vote
2 answers

NoClassDefFoundError For IClass$DefaultImpls when writing default method for interface in Kotlin

I have an interface MyInterface in Kotlin with some methods that some of them have a default implementation. Also, I have another interface IClass that extends MyInterface written in Java. At last, I have a class MyClass that implements MyInteface…
Hossein Nasr
  • 1,436
  • 2
  • 20
  • 40
1
vote
2 answers

Is using default method in an interface a good pattern to avoid code duplications?

We have a lot of code duplication in data holder classes that can be serialized to a XML string: public String toXml() throws JAXBException { final JAXBContext context = JAXBContext.newInstance(this.getClass()); final Marshaller marshaller =…
AndreasL
  • 21
  • 3
1
vote
1 answer

Why I can not create a default method in an interface?

I'm just a newbie and learning about interfaces. I've tried to read some queries here before posting this. But it seems that it's too complex for me. And they're talking about default Object methods. In my case, I'm trying to create a default method…
thox
  • 129
  • 4
  • 13
1
vote
4 answers

Calling a default method from Interface in Main Class

I am having a problem with calling a default method from an interface. Here is my code: Interface: public interface Pozdrav { public static void stat(){ System.out.println("Ja sam staticni metod"); } default void osn(){ …
1
vote
1 answer

Default method is invisible during Camel Simple evaluation

Issue can be reproduced with below unit test, I have not found it in tracker. Essence: interface A has default method, interface B extends A, default method is invisible during Simple language evaluation. Camel version 2.16.1. Am I missing…
Yamahar1sp
  • 510
  • 1
  • 6
  • 13
1
vote
3 answers

Is this an acceptable use of a Java default interface method?

public interface Example { E get(int index); default E get() { return get(0); } } This would be the initial design of the interface. Is something like this an acceptable use of a default method (basically for default…
sep1256
  • 55
  • 6
1
vote
1 answer

Error generated when tried to call superclass method from the main method of the program

I'm new to Java and is trying to learn the concept of inheritance. When I tried to call the EggLayer's identifyMyself() method from the main class's identifyMyself method() using System.out.println(EggLayer.super.identifyMyself()); it works as…
Thor
  • 9,638
  • 15
  • 62
  • 137