Does the super class not call the overridden method?

14,360

Solution 1

Are your two classes in different packages? And is your foo class methods declared public, protected, or private or package local? Obviously if they are private, this won't work. Perhaps less obvious, is if they are package local (i.e. no public/protected/private scope) then you can only override them if you are in the same package as the original class.

For example:

package original;
public class Foo {
  void a() { System.out.println("A"); }
  public void b() { a(); }
}

package another;
public class Bar extends original.Foo {
  void a() { System.out.println("Overwritten A"); }
}

package another;
public class Program {
  public static void main(String[] args) {
    Bar bar = new Bar();
    bar.b();
  }
}

In this case, you will still get 'A'. If you declare the original a() method in Foo public or protected, you will get the result you expected.

Solution 2

It may be that you are trying to use static methods, which won't work as they don't get overridden.

A good way of checking is to add the @Override annotation to bar.a() and see if the compiler gives you an error that a() isn't actually overidding anything

Solution 3

When I run the following:

public class Program {
    public static void main(String[] args) {
        bar b = new bar();
        b.b();
    }
}

class foo {
    public void a() {
       System.out.printf("a");
    }
    public void b() {
        a();
    }
}

class bar extends foo {
    public void a() {
        System.out.printf("overwritten a");
    }
}

I get the following output:

overwritten a

which is what I would expect to see.

Solution 4

Are the methods defined as static? That's the only way I could see getting that result. I found a good explanation about that here: http://faq.javaranch.com/view?OverridingVsHiding

Share:
14,360

Related videos on Youtube

Rob
Author by

Rob

Author of Rodgort - A project designed to make managing burnination, synonym and merge requests easier on Stack Overflow Advanced Flagging - A userscript to aid with moderation. Higgs - A work in progress to provide web interfaces for bots in SOBotics

Updated on April 17, 2022

Comments

  • Rob
    Rob about 2 years

    I have the following classes:

    class foo {
        public void a() {
            print("a");
        }
        public void b() {
            a();
        }
    }
    
    class bar extends foo {
        public void a() {
            print("overwritten a");
        }
    }
    

    When I now call bar.b() I want it to call the overridden method a() in foo. It does, however, print "a".

    • brady
      brady about 15 years
      Pardon the didactic: It's called "overriding", not "overwriting".
  • Eddie
    Eddie about 15 years
    You beat me by one minute. I get the same output.
  • Adeel Ansari
    Adeel Ansari about 15 years
    May be he beat you in posting, otherwise, I believe, we got this output way before this question is posted.
  • shieldgenerator7
    shieldgenerator7 over 8 years
    This was my problem; changing the supertype method to protected fixed it. What I don't get is why the @Override tag on the subtype method didn't make the compiler give me an error message if it wasn't going to work.