Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:17:04+00:00 2026-06-02T13:17:04+00:00

Suppose I have an inheritance chain where every class extends its superclass by adding

  • 0

Suppose I have an inheritance chain where every class extends its superclass by adding a new field and I want every class of that chain to override the toString() method like so:

public String toString()
{
    return super.toString() + "[newfield=" + newfield + "]";
}

If I use an abstract base class then when a class of the inheritance chain becomes concrete by implementing the abstract method all the subclasses from that point on become concrete as well by inheriting the already implemented method.

Is there a way in Java to force every class to override (reimplement) the abstract method even though it has already been implemented higher in the inheritance chain?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:17:05+00:00Added an answer on June 2, 2026 at 1:17 pm

    Short answer: no.

    Long answer: you could write a unit test that scans the class path (use a library like reflections for that), loads every subclass of the abstract class and checks for the method using reflection. But there’s no way to do it at compile time.

    Aside from Java,
    AspectJ has a hasMethod() pointcut, which could do the check for you, but unfortunately that’s only valid for the declare parents advice. Perhaps you could do it in two steps using aspectj:

    • define an interface IllBehaved.
    • define an advice that assigns this interface to classes that extend from your base class but don’t have the member
    • then declare a compile error for all types that implement this interface

      pointcut isSubTypeOfYourClass(): within(com.your.BaseClass+);
      pointcut overridesMethod(): hasmethod(public void yourMethodName(..));
      declare parents: isSubTypeOfYourClass() && !overridesMethod()
                       implements com.yourcompany.IllBehaved;
      declare error: isSubTypeOfYourClass() && within(com.yourcompany.IllBehaved+):
                       "Implementation class must override <Foo> method";
      

    I haven’t tested this, but it should work if you turn on the -XhasMember option.
    And it’s easy enough to integrate aspectj into your build when you use standard tools like Maven, Ant, Eclipse etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a C++ program that has excessively deep inheritance as follows: using
Suppose I have a class that downloads a news feed from the web public
Suppose I have a diamond inheritance situation as follows: class A{ public: virtual void
Suppose I have a class that looks like this: class Derived : // some
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Suppose I have string Name and Image Photo as properties of a class in
Suppose I have a list, in which no new nodes are added or deleted.
Suppose you have a subsystem that does some kind of work. It could be
Suppose I have a small inheritance hierarchy of Animals: public interface IAnimal { string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.