0

I have a question about overriding methods. if i have class a that has a method that can be overriden by the subclasses but is called in the superclass

public class a {
  //this is the main class
  public a() {
       //etc code
       callMethod();
   }
   void callMethod() {
       //do nothing here. will be overriden in subclas
  }

  public class b extends a {
      void callMethod() {
           //actually impliment here
  }
  }

how to I call class b's implimentation of the method from class a

  • 3
    Neither of your classes extends the other, so there's no overriding here... – Jon Skeet Oct 19 '11 at 17:46
  • 1
    Note: It's recommended to start class names with a capital letter and not to call overrideable methods in constructors. – Puce Oct 19 '11 at 17:51

5 Answers5

1

Probably you meant:

public class b extends a

In this case the code:

a aa = new b();

Will call b.callMethod(). Note that calling overriden (non-private) methods from a constructor is a bad idea.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • its not actually from the constructor its from another method – Jeremy Kaplan Oct 19 '11 at 17:50
  • As far as I can see You have a class named `a` with a method named `a` and no return value - this **is** a constructor calling `callMethod()`. Except that your could run just fine, or maybe I don't understand the question? – Tomasz Nurkiewicz Oct 19 '11 at 17:52
0

If you change the visibility of the callMethod method to protected then b.callMethod() is what would be called, even from the superclass.

The protected keyword tells Java that the method is visible to subclasses; and by extension makes it virtual.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • yeah but it needs to be called from the superclass – Jeremy Kaplan Oct 19 '11 at 17:47
  • @Jeremy: And I'm telling you it will work fine if it's protected. The semantics on default access members are a little more blurry. – Mark Peters Oct 19 '11 at 17:49
  • but the superclass does not know the name of the subclass – Jeremy Kaplan Oct 19 '11 at 17:54
  • @Jeremy: It doesn't need to. That's the nature of overriding...since your `A`'s concrete class will be `B`, it will use the implementation found in `B`. I would appreciate if you would try it rather than just not believing me. Then if you still have questions or you find I'm wrong, keep asking. – Mark Peters Oct 19 '11 at 17:55
  • i tried it but it didn't work. I am not sure if I did something wrong. The superclass method was protected and the subclasse method was protected. I called the method from within the superclass but it did not run the subclass version – Jeremy Kaplan Oct 19 '11 at 18:00
  • @Jeremy: Please post your exact code in the form of an [SSCCE](http://sscce.org). – Mark Peters Oct 19 '11 at 20:22
  • 1
    I ended up getting it to work. turns out after i put the protected in, I put the overriding method in the wrong class, but I figured it out. Thanks for all your help – Jeremy Kaplan Oct 20 '11 at 19:09
0

The following results in a compiler error: "An enclosing instance that contains Demo.A.B is required." That is discussed in: An enclosing instance that contains <my reference> is required. So, the answer is: You can't do that. Write it into its own class file instead of a nested class.

package Demo;

import Demo.A.B;

public class Demo {
    public static void main(String[] args) {
        A test = new B();
    }
}

Broken

Package Demo;

public class A {

    public A() {
        callMethod();
    }

    void callMethod() {
        System.out.println("Called from A");
    }

    public class B extends A {
        @Override
        void callMethod() {
            System.out.println("Called from B");
        }
    }
}

Working

(A.java)

Package Demo;

public class A {

    public A() {
        callMethod();
    }

    void callMethod() {
        System.out.println("Called from A");
    }
}

(B.Java)

package Demo;

public class B extends A {
    @Override
    void callMethod() {
        System.out.println("Called from B");
    }
}
Community
  • 1
  • 1
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
  • so here is how my program actually works. I have one class that provides by root functionality for all my other classes. One of the functions the root class peforms is sliding in and out my panels. I need the superclass method that slides in and out the panels to call the subclass implimentation of the method that will run when the panels are finished loading. – Jeremy Kaplan Oct 19 '11 at 17:52
  • @JeremyKaplan It sounds like you've got a design issue. When you say panels, are you talking about panels in a windowing system (Swing / AWT)? Why do they need to be the same name / be an override? – Jeff Ferland Oct 19 '11 at 17:54
  • they are not the same name. I have the superclass controlling the sliding in and out of the panels. Each of my subclasses extends the superclass. – Jeremy Kaplan Oct 19 '11 at 17:56
  • I meant same method name, but ok.... we're making progress. As an override, if you do `A a = new B()`, then `a.callMethod()` will be the method from `B`. You simply need to instantiate the right object. – Jeff Ferland Oct 19 '11 at 17:59
  • the only issue with this is that b is not created within A, it is created within a whole different class. – Jeremy Kaplan Oct 19 '11 at 18:03
0

you cannot but you can create constructor for class b, call workflow will be next fileds a - > constructor a - > method from a-> constructor b - > method from b

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
0
public abstract class a {
  //this is the main class
  public a() {
       //etc code
       callMethod(); 
   }
   abstract void callMethod();
}

  public class b extends a {
      void callMethod() {
           //actually impliment here
  }
}

But don't do this. This calling method from constructor in superclass a maintenance nightmare and bad design.

ollins
  • 1,851
  • 12
  • 16