0

Lets say I have a class called SuperClass:

public class SuperClass
{
    protected void TheCake()
    {
        System.out.println("The cake is a lie");
    }
}

and another called SubClass:

public class SubClass extends SuperClass
{
    @Override
    protected void TheCake()
    {
        System.out.println("The cake is not a lie");
    }
}

In a third class I have this method

public void GetCake(SuperClass someObject)
{
    Class clazz = someObject.getClass();
    Method method = clazz.getMethod("TheCake");
    method.invoke(someObject, (Object[])null));
}

This would never work, because TheCake is protected. So if I change this line:

Method method = clazz.getMethod("TheCake");

to this:

Method method = clazz.getDeclaredMethod("TheCake");

It would only work if someObject was an instance of SuperClass, and fail if it was an instance of SubClass, because off course the method is declared in SuperClass.

But what if I did this instead?

public void GetCake(SuperClass someObject)
{
    Class clazz = SuperClass.class;
    Method method = clazz.getDeclaredMethod("TheCake");
    method.invoke(someObject, (Object[])null));
}

If someObject were an instance of SubClass, what method would be invoked? The SuperClass definition of the method or the SubClass override of the method?

user2212990
  • 177
  • 1
  • 9
  • What happens when you try it? – Eric Stein Aug 16 '13 at 16:31
  • 2
    Trying this out seems like a far easier way to find out than typing up this entire question :-) – Sergey Kalinichenko Aug 16 '13 at 16:31
  • 1
    I can't try because I'm not at school and the java development environment I have at home is my dad's and it's cluttered with the packages of his project and and affraid of messing something up. I'm not good at java, I understand the language fairly well but I'm not experienced in using the IDE, I just need java because I have classes on it at school. C# is more in my confort zone. – user2212990 Aug 16 '13 at 16:36
  • You could give it a go outside of an IDE by putting that code into .java files and compiling them in the command line. – Surveon Aug 16 '13 at 16:54
  • That's a good idea, I'll look up on how to do that. – user2212990 Aug 16 '13 at 16:58
  • shouldn'T subclass extends superclass? – Philipp Sander Aug 16 '13 at 17:01
  • Took me some 30 minutes to write such simple code without an IDE but the good news is it works perfectly. I get the declared method from the super class and when I invoke it on an instance of a subclass what I really invoke is the overriden version of the method. Thanks everyone. – user2212990 Aug 16 '13 at 17:33

1 Answers1

0

If your 'other class' is in the same package as the SuperClass and SubClass
the following will work:
(exeption handling omitted)

public void GetCake(SuperClass someObject) {
    Class clazz = someObject.getClass();
    Method method = clazz.getDeclaredMethod("TheCake");
    method.invoke(someObject);
}

If you want this code to work from another package you can modify the method access with method.setAccessible(true);

Your final method will look like this:

public void GetCake(SuperClass someObject) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    Class clazz = someObject.getClass();
    Method method = clazz.getDeclaredMethod("TheCake");
    method.setAccessible(true);
    method.invoke(someObject);
}
akkerman
  • 119
  • 6