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
1
vote
2 answers

UML: Java Defender or Default Methods

Since the release of Java 8 you may provide default implementations of methods in interfaces. I have been looking for a way to realize this in UML but could not find anything on the matter. The case of default implementations in interfaces probably…
timbernasley
  • 490
  • 9
  • 21
1
vote
2 answers

default method in interface runs with command prompt but not in eclipse

interface G { default void print() { System.out.println("G"); } } class M { public void print() { System.out.println("M"); } } class GImpl extends M implements G {} public class Wierd { public static void…
saurabh kumar
  • 155
  • 5
  • 26
1
vote
1 answer

prevent inheritance of interface outside of package

I have a stateless abstract base class that should not be inherited from outside of its package: package foo; public abstract class Foo { // some abstract methods // one concrete method // no state // Prevent classes outside of…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
1
vote
4 answers

java default method won't work

I have recently upgraded to java and I am trying my hand at the new default methods for interfaces. However I keep getting the syntax error on token "default", delete this token. Here is my build path where I would expect a problem to occur: I…
Thijser
  • 2,625
  • 1
  • 36
  • 71
0
votes
1 answer

Java 17: MenuIterator is not abstract and does not override abstract method remove() in java.util.Iterator

I'm getting the following error while implementing the java.util.Iterator interface in my class: java: MenuIterator is not abstract and does not override abstract method remove() in java.util.Iterator. import java.util.Iterator; public class…
0
votes
2 answers

How can I write a junit test case for default methods of interface with zero lines of code

How can I write a junit test case for default methods of interface with zero lines of code as mentioned below : public interface A{ default void method1(){ } }
Theodore
  • 57
  • 1
  • 7
0
votes
1 answer

can abstract method do System.out.println?

I am a beginner in Java. I have a question about the abstract method in Interface. Is the following sampleMethod() an abstract method? My understanding is that it is not an abstract method due to System.out.println. Am I correct? default void…
Lei
  • 11
0
votes
1 answer

Selecting default implementation from indirectly inherited Interface not working

I have 4 Classes that look like this: public interface Foo { ... default boolean isEmpty() { return false; //dummy value real implementation is not relevant for the problem } } public interface CharSequence {…
RedCrafter LP
  • 174
  • 1
  • 8
0
votes
1 answer

Is it possible to implement native methods in interfaces?

You often hear that methods in an interface have no implementation. However, in Java 8 it became possible to implement the default methods. But I'm interested. Was and is it possible to implement interface methods natively? (native methods). When…
stfxc
  • 174
  • 6
0
votes
0 answers

cannot call default method of parent interface using super

interface WithDefinitionsInter { default void definedMeth() { System.out.println("inside interface"); } } class WithDefinitionsImpl implements WithDefinitionsInter { public void definedMeth() { super.definedMeth(); //…
Manish Bansal
  • 819
  • 2
  • 8
  • 19
0
votes
0 answers

concrete method in abstract class and default method in interface having the same name

I have declared the default method and concrete method in the abstract class with the same name. public class AbstractClassDefaultMethodTest { public static void main(String[] args) { DemoInterface testDefaultMethods=new…
0
votes
1 answer

Mutilple inheritance strange behavior - Java 8

In the following code if I uncomment I3 and implements I2 and I3 then there is a compilation failure with the following error: unrelated defaults for m2() from I3 and I2 Which is as fine and expected behavior. However, when I replace I3 with I,…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
0
votes
1 answer

Is there a way to inject an object into an interface using Dagger2

In my activity class, I can inject a ViewModel using: @Inject GameViewModel gameViewModel; And it works fine. The problem is, that I want to use the object in an interface: public interface SharedData { @Inject GameViewModel…
0
votes
0 answers

Need some suggestion for keyboard hiding code

I always found one problem whenever i make forms (for example, login or registration form) in my applications, that problem is hiding keyboard when user clicks on submit button. For solving this problem i always write keyboard hiding code in the…
0
votes
0 answers

@JvmDefault not found sourceCompatibility/targetCompatibility assigned by project variable

I want to provide default implementation for method in my Kotlin interface: interface INavItem : Comparable { val order: Int @JvmDefault override fun compareTo(other: INavItem): Int = order.compareTo(other.order) } but…