DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+

Trending

  • Load Testing Essentials for High-Traffic Applications
  • Advancing DSLs in the Age of GenAI
  • Top 5 Trends in Big Data Quality and Governance in 2025
  • Lessons Learned in Test-Driven Development
  1. DZone
  2. Coding
  3. Languages
  4. Why a Default Method Cannot Override an Object’s Methods? [Snippet]

Why a Default Method Cannot Override an Object’s Methods? [Snippet]

Wanting to understand why a default method cannot overrise an object's methods? Click here for the code snippet that explains why.

By 
Aymen Abidi user avatar
Aymen Abidi
·
Oct. 12, 18 · Code Snippet
Likes (7)
Comment
Save
Tweet
Share
19.7K Views

Join the DZone community and get the full member experience.

Join For Free

An interface cannot declare any of the methods of the object class as a default method. This restriction may be surprising, especially since the interface does not inherit from object.

Behind the scenes, an interface implicitly declares a public abstract method for most of the object’s method. As a consequence, there is no hierarchic relation between, for example, the object's equals method and the equals method that is implicitly declared in an interface. So, why does Java prohibit us from declaring an object method as the default method?

The short answer is this: Java, by design, can be misunderstood from the OOP design perspective:

interface A {

    default boolean equals(Object obj) {
        return true;
    }
}

class B implements A {

}


Let’s suppose we have an interface, A, which declares a default method equals with the same signature as the equals method of the object class. Class B  implements the A interface, which, by design, inherits the object class, and then, what version of the equals method does B inherit? A conflict would arise in this case.

Another reason is that, in this case, the interface default method becomes useless, because the equals method from the object class will always be invoked in children classes. This would conflict with the fact that the interface can be evolved. Hope this answered your question. Happy coding!

Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: