5

I have the following :

public Class A {

    public void methodA() {
        ....
    }

    public void methodB() {
        ....
    }

}

I don't have control over Class A. I would like to create subclasses of Class A with a restriction that it must override methodA() (super.methodA() call must run the implementation of Class A). is it possible? is so, how?

  • You create the subclass, so you get to decide what to override or not to override. What are you trying to achieve? Are you really talking about sub-subclasses, perhaps? – Kilian Foth Aug 1 '13 at 15:30
  • I think so. A subclass of Class A which imposes such restrictions for sub-sub classes? – 500865 Aug 1 '13 at 15:32
6

First thing to understand here is that this part of requirements: super.methodA() means this applies to the code of the classes extending A, in a way defined in JLS 15.12.4.4-2. Method Invocation Using super

An overridden instance method of a superclass may be accessed by using the keyword super to access the members of the immediate superclass, bypassing any overriding declaration in the class that contains the method invocation...

Let's say, we want a way to create some specific subclasses of A such that whenever super.methodA() is invoked in these, we are guaranteed that instead of code that was in class A (out of our control) it will run some other code, defined in some subclass of A (under our control).


Let's see what "tools" are at our disposal to achieve above.

  1. Need to create some, specific subclasses of A brings to mind traditional way to specialize - subclassing, that is we'll consider subclass of A (let's call it B) that would redefine (override) methodA in the way we want.

  2. Need to force a subclass to provide a code that would run where we want it in superclass can be satisfied by defining an abstract method, let's call it forcedImplementationInSubclass()


Now we've got all what we need.

  • We will define a class B extending A, so that all subclasses of B will also be subclasses of A.
  • In B, we will override methodA, so that its implementation in class A won't be available to subclasses of B anymore.
  • In overridden method in B we will invoke abstract method forcedImplementationInSubclass() which will guarantee that subclasses of B will have to define the code that will eventually run in methodA.

As a result, any subclass of B won't be able to invoke methodA implementation provided in class A and instead, will be guaranteed to run code defined in some subclass of B.

Extending B is the way to create specific subclasses of A that behave as we want. The code could look about as follows:

public abstract class B extends A {
    // override, so that subclasses of B won't be able to get to super.methodA
    @Override public void methodA() {
        // invoke abstract method here (subclass would have to implement it)
        forcedImplementationInSubclass();
    }

    // define abstract method so that subclass would have to implement it
    protected abstract void forcedImplementationInSubclass();
}

// extending B is the way to get what we need
public class C extends B {
    // enforced implementation of the abstract method
    @Override protected void forcedImplementationInSubclass() {
        // that will be invoked when B.methodA() is called, and since B is-a A...
        System.out.print("Hello from implemented methodA");
    }

    public void testSuperMethodA() {
        // that will invoke B.methodA() which won't go to class A
        super.methodA();
    }

    public static void main(String[] args) {
        A a = new C();
        a.methodA(); // goes to B.methodA(), which in turn invokes C.forcedImplementationInSubclass()
        C c = new C();
        c.testSuperMethodA(); // won't go to class A
    }
}
4

Once an method has been given an implementation, there is no way of making it abstract again. So, no, without changing A there is no way of writing a class B extends A so that all subclasses of B must provide another implementation, at least not via the type system.

The best you can do is write a method in B that throws an Exception at run time. This forces any subclass to override the method to be useful, but it will still compile without doing that.

  • If I throw an exception at runtime in Class B. How can I call the super method in the subclasses of Class B? I would like the super call to run the logic in Class A. – 500865 Aug 1 '13 at 15:46
  • super will only call the implementation of the direct superclass (if that has no method, the compiler will implicetely create one with a single super statement). So indirectly the implementation of A will be called, if and only if the method in the subclass does use a super statement. – Jim Martens Aug 1 '13 at 16:17
  • @500865 Can your class B implement methods as final and inside them call super()? – Daenyth Aug 1 '13 at 16:33
-1

I think you are talking about 'abstract class'. If you want that the subclass of Class A must override the methodA() of Class A, then you have to declare the methodA() as abstract method and Class A is an abstract class.

If any class method having some implementation then it will act as a default implementation of the subclasses. Subclasses can optionally override that with their own implementation. If you want to give implementation to the subclass only and force the subclasses to follow a contract (method signature) then this is a clear situation of abstract class.

class A
{
    void abstract methodA(); // No implementation.
}

class B extends A
{
    void methodA()
    {
        // Subclass implementation.
    }
}
  • 2
    OP stated that he/she has no control over class A, so she/she cannot make its methods abstract. – Tulains Córdova Aug 1 '13 at 15:37
  • If methodA() has implementation in base class then it is normal to call that implementation for the derive classes. There is no other solution in normal object oriented process. – DeveloperArnab Aug 1 '13 at 15:42
  • 1
    You should state in your question that "there is no other solution in normal object oriented process", as you stated in your comment. – Tulains Córdova Aug 1 '13 at 15:44
-1

Before using reflection API think about the cost of it.

It is simply easy to do. For instance:

C subclass of B and B subclass of A. All three have a method methodName() for example.

public abstract class A {

    public void methodName() {
        System.out.println("Class A");
    }

}

public class B extends A {

    public void methodName() {
        super.methodName();
        System.out.println("Class B");
    }

    // Will call the super methodName
    public void hackSuper() {
        super.methodName();
    }

}

public class C extends B {

    public static void main(String[] args) {
        A a = new C();
        a.methodName();
    }

    @Override
    public void methodName() {
        /*super.methodName();*/
        hackSuper();
        System.out.println("Class C");
    }

}

Run class C Output will be:

Class A 
Class C

Instead of output:

Class A 
Class B 
Class C
  • 2
    What does this answer add that is not in the accepted answer from a year and a half ago? – user22815 Feb 16 '15 at 15:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.