Java class to enforce an implementation of super classes’ public methods

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?

2

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
    }
}

2

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.

3

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.
    }
}

3

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

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật