Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a Java Method

I'm new to Java, and I've read over some tutorials on overriding methods, but an example I'm looking at isn't working the way I expect. For example, I have the code:

public class A{
    public void show(){
        System.out.println("A");
    }
    public void run(){
        show();
    }
    public static void main( String[] arg ) {
        new A().run();
    }
}
public class B extends A{
    @Override
    public void show(){
        System.out.println("B");
    }
}

When I instantiate and call B.run(), I would expect to see "B" outputted. However, I see "A" instead. What am I doing wrong?

Edit: Yes, the classes are in two separate files. They're shown together for brevity.

Edit: I'm not sure how B is being instantiated, as it's being done by a third-party program using a classloader.

Edit: More info on the third-party program. It starts by calling A.main(), which I didn't initially show (sorry). I'm assuming I need to make "new A().run();" more generic to use the name of the current class. Is that possible?

like image 860
Cerin Avatar asked Jan 28 '10 13:01

Cerin


2 Answers

That code will output B if you:

(new B()).run();

Whatever the problem is, it's not in the code you've quoted.

Updated (after your edit)

If the third-party program is calling A.main(), there's nothing (reasonable) you can do in B that will inject itself into A. As long as A.main is doing new A().run(), it's going to have an instance of A, not an instance of B. There's no "current class name" to use, or if there is (depends on your point of view), it's A, not B.

You'll have to get the third-party program to call B in some way, rather than A, or just modify A directly (e.g., getting rid of B entirely). You do not want to modify A to make it use B; that tightly binds it to a descendant and makes the separation between them largely pointless.

Hope that helps.

like image 196
T.J. Crowder Avatar answered Oct 11 '22 21:10

T.J. Crowder


I tried, putting your two classes in two files, and it worked nicely, outputting "B". I called :

 B b = new B();
 b.run();

UPDATED : Also works as (because it is the same runtime instance):

 A a = new B();
 a.run();
like image 30
KLE Avatar answered Oct 11 '22 21:10

KLE