Default Methods

suggest change

Versions

Java SE 8. Java SE 9 was early access when this topic was written. Default methods were added in Java 8.

Introduction

Default methods, introduced in Java 8, add a method to an interface without breaking existing implementors. The interface supplies an implementation used when a class does not implement that method.

Syntax

Remarks

Default methods

Static methods

A subclass method interacts with a superclass method of the same signature as follows:

Superclass instance method Superclass static method
Subclass instance method overrides compile-time error
Subclass static method compile-time error hides

An implementing class interacts with an interface method of the same signature as follows. An interface static method is not inherited, so a class method neither overrides nor hides it:

Interface default method Interface static method
Class instance method overrides unrelated; call the static method on the interface
Class static method compile-time error unrelated; call the static method on the interface

References :

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents