Java – How To Invoke Parent Class Methods using Java Reflection

This post shows you how to call or invoke methods of parent/super class by using Java Reflection.

Let’s look at the below code example to see how it works.
package com.bytenota;

import java.lang.reflect.Method;

class A extends B {
    public void sayHello() {

class B {
    public void sayGoodbye() {

public class ReflectionExample {
    public static void main(String[] args) throws Exception {
        // create a new instance 
        Object aInstance = Class.forName("com.bytenota.A").getDeclaredConstructor(null).newInstance(null);
        // invoke sayGoodbye() method of super class B
        Method method = aInstance.getClass().getSuperclass().getDeclaredMethod("sayGoodbye", null);
        method.invoke(aInstance, null);

In the above code, we create class A that extends class B, then use reflection to invoke sayGoodbye() method of class B from an instance of class A.