From the course: Java SE 17 Developer (1Z0-829) Cert Prep

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Default method resolution

Default method resolution

- Now let's look at default method resolution. There are quite a few rules for default methods. One of them is that a default method must not match the signature of any method of java.lang.object. So in this example, you can see that in our interface W, we have a boolean equals method that takes an object as an argument. So this is equivalent to the equals method of java.lang.object. As an abstract method, that's entirely valid. However, if we try to make it into a default method, then the compiler objects. We're not allowed to have a default method that would effectively override a member of java.lang.object. Another rule is that a default method provides what amounts to a fallback implementation only. Any implementation of the same signature in the class hierarchy will always be used in preference. So here our interface W declares a default void do stuff with no arguments method. That will print out its name, so we know if that one is invoked. We have a Class A, which claims to…

Contents