Is there a way to call a parent class method from a child class object in java without modifying the methods

I have a parent class and a child class both of which have an m1 method with the same signature (override), can I call the parent class method in the following scenario. I don't want to change the method of the child class.

// Super class
public class Parent
{
    public void m1()
    {
        System.out.println("Parent method");
    }
}
// Sub class
public class Child extends Parent {
    @Override
    public void m1() {
        System.out.println("Child method");
    }
}
// User class
public class Kavi {
        public static void main(String[] args) {
            Parent p = new Child();
            p.m1();

        }
}

      

I want to call a method of the parent class m1. I know that I can use the super method in the child class to call its parent method. but I am not allowed to modify the source code of the child class. and I have to call it from a class object. please someone help !!! is this possible in java ??

+3


source to share


4 answers


When you create an object, you are using a Super class reference, but your object has a child class, so when you call the m1 () method, the override method is called. If you want a superclass method to be called, the object must be of class Super. How:

Parent parent=new Parent();
parent.m1();

      

or

you can call super class method m1 () from child class.



@Override
public void m1() {
    super.m1();
    System.out.println("Child method");
      }

      

OR ELSE:

   import java.lang.reflect.*;
class A {
    public void method() {
        System.out.println("In a");
    }
}
class B extends A {
    @Override
    public void method() {
        System.out.println("In b");
    }
}
class M {
    public static void main( String ... args ) throws Exception {
        A b = new B();
        b.method();

        b.getClass()
     .getSuperclass()
     .getMethod("method", new Class[]{} )
     .invoke(  b.getClass().getSuperclass().newInstance() ,new Object[]{}                  ) ;

}
}

      

+3


source


I think it's impossible. There are two ways to call a parent class method

1.) Parent class object box as

Parent p = new parent ();



2.) Use super method in child class method like

@Override
    public void m1() {
        super.m1();
        System.out.println("Child method");
    }

      

+3


source


Without changing the code, you cannot do this. You are, in essence, talking about p.super.m1()

which is not legal in Java. If you want your parent to act like a parent, don't make it a child.

If both parent and child are stateless, you can create a facade over them and manipulate state explicitly; this will work, but I would not recommend it.

public class Facade extends Parent {

    public enum State {PARENT, CHILD};

    private final Child delegate;

    private State state = State.CHILD;

    public Facade(Child delegate) {
        this.delegate = delegate;
    }

    @Override
    public void m1() {
        if (State.CHILD == state) {
            delegate.m1();
        } else {
            super.m1();
        }
    }

     public void setState(State state) {
         this.state = state;
     }
}

      

This is a purely academic exercise - I can't think of any good reason to do this in the real world. If you're using the OO language, don't fight the OO paradigm!

+3


source


If you cannot use super

, then instead of creating an object of the child class, you can directly use

Parent p = new Parent();
p.m1();

      

if you cannot even change the code inside the main method, then I think it is not possible.

0


source







All Articles