Nick Moran
Nick Moran

Reputation: 83

What does the Java specification mean when it says that interfaces can be used to add methods to Object?

In the introduction to Chapter 9, the Java Language Specification says:

"Programs can use interfaces to make it unnecessary for related classes to share a common abstract superclass or to add methods to Object."

https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html

What does the bolded section mean? How can one use an interface to add methods to Object?

To be clear, I'm not asking for an explanation of what an interface is, or an explanation of the non-bolded section of the quoted sentence, only an explanation of what is meant by "add methods to Object".

Upvotes: 0

Views: 39

Answers (2)

kr1tzy
kr1tzy

Reputation: 148

It's not worded the best at all. In layman terms, it's a layer that allows you to write method signatures that your classes must fulfill in addition to what the object class has. So when your class extends the object class it isn't only required to fulfill the contract of the default methods - hashing, to string, etc; it's also required to fulfill methods of whatever interface(s) it inherits. Helps organization, architecture, sanity, etc.

Upvotes: 0

user207421
user207421

Reputation: 311054

It doesn't say that. It says they can be used to make it unnecessary to add methods to Object.

Upvotes: 3

Related Questions