Hello,

How could this be possible:

SuperClass has a method named executeMe(), Subclass1 that extends Superclass has also method named executeMe(), Subclass2 that extends SuperClass has also the same method. When I try to call the executeMe() method from SuperClass, all classes that has been called with the method executeMe() will run without calling the method within the class.

Same concept as of paint(Graphics g) in j2me when a class extends Canvas. Any class that extends Canvas having the method paint() will be automatically called.

Thank you in advance!

Dani AI

Generated

As pointed toward, the behavior almost always comes down to Java's method dispatch rules rather than anything "magic" that calls every subclass. Instance methods in Java are virtual: when an instance's executeMe() is invoked the runtime type's override runs. The common GUI example (paint(Graphics)) is not a special language feature — the GUI framework simply calls paint on each component instance when it repaints the screen.

Common causes and quick checks:

  • Verify whether executeMe is declared static. Static methods are resolved at compile time (method hiding), not overridden at runtime.
  • Use the @Override annotation on subclass methods to catch signature mismatches (overloading vs overriding).
  • Search the codebase for loops, listener/registration lists, or framework code that might be iterating through multiple instances and calling executeMe() on each (that would produce the “all classes run” effect).
  • Add a simple runtime print or breakpoint inside executeMe() that prints getClass().getName() to see which object is being invoked.

Minimal example illustrating runtime dispatch vs static hiding:

class Super {
    void executeMe() { System.out.println("Super"); }
    static void staticExecute() { System.out.println("Super static"); }
}

class Sub extends Super {
    @Override
    void executeMe() { System.out.println("Sub"); }
    static void staticExecute() { System.out.println("Sub static"); }
}

public class Test {
    public static void main(String[] args) {
        Super s = new Sub();
        s.executeMe();          // prints "Sub"
        Super.staticExecute();  // prints "Super static"
    }
}

For authoritative reading on overriding and polymorphism, see Oracle's Java tutorials on Overriding methods and Polymorphism. This should help identify whether the observed behavior is normal dynamic dispatch, static method hiding, or an explicit loop/registration that invokes every implementation.

Recommended Answers

All 3 Replies

parameters in the method may differentiate your calling..

try to look difference between overloading and overriding..

than you know what to do..

parameters in the method may differentiate your calling..

try to look difference between overloading and overriding..

than you know what to do..

Thank you! :)

if it is useful then add reputation point for me and mark as solved...

Thank you!!!!!!!!!!!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.