Results 1 to 2 of 2
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Can an extended class "know" if a certain function from the base class is overridden?

    I have a base class with a dummy function call to allow editing of class fields.

    Let's call the method editClassFields().

    In practice, all objects extend the class to add extra fields, methods, etc. but not all of them have a need to call editClassFields(). In those cases I don't override the base editClassFields() method.

    What I'm looking for is a way for the extended classes to "know" whether or not the editClassFields() has been overridden, so that they can display themselves slightly differently. This lets the user know (by a subtle change in appearance) that they can edit the fields.

    Coming from the world of C/C++, I still have this concept in my mind of "function pointers" and so thinking along those lines I could compare

    this.editClassFields() to super.editClassFields(). And if they were the same, then the base method would not have been overridden.

    What's a good way to do this in Java without requiring me to add another field indicating whether or not the base method has been overridden?



  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: Can an extended class "know" if a certain function from the base class is overrid

    Well Java has no function pointers (thank god), but it does have something called reflection to get runtime information about class structures. Through that you may be able to figure out when a method is in a parent class only. This whole requirement invokes such a huge 'do not want!' in me that I'm not even going to make the effort to try and piece it together. Here is a piece of code which I use to know if a class has a certain property or not, perhaps you can adapt it to your needs:

    Java Code:
        public static boolean hasPrivateProperty(Object object, String fieldName) {
            Class<?> clazz = object.getClass();
            while (clazz != null) {
                Field field;
                try {
                    field = clazz.getDeclaredField(fieldName);
                    if (field != null) {
                        return true;
                } catch (SecurityException e) {
                    throw new RuntimeException(e);
                } catch (NoSuchFieldException e) {
                    /* ignore (and try to find the field in the superclass) */
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException(e);
                clazz = clazz.getSuperclass();
            return false;

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 6
    Last Post: 02-08-2012, 06:42 PM
  3. Replies: 0
    Last Post: 08-19-2011, 01:19 PM
  4. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  5. Replies: 6
    Last Post: 05-18-2010, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts