Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way to call parent class method from child class object in java without modifying methods

I have parent class and a child class, both of having a method m1 with same signature (Override), can I call parent class method in following scenario. I dont want to change child class method.

// 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 parent class m1 method. I know that I can use super in child class method to call its parent method. but I have no right to change the source code of child class. and I have to call it from child class object. please anybody help !!! is it possible in java ??

like image 880
kavi temre Avatar asked May 06 '15 05:05

kavi temre


4 Answers

While creating the Object you are using reference of Super class but your object is of child class, so while calling m1() method the overrided method will be invoked. If you want the method of the super class to be invoked then object should be of Super class. As :

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

OR

you can invoke the super class m1() method from the 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[]{}                  ) ;

}
}
like image 170
Charles Stevens Avatar answered Nov 20 '22 16:11

Charles Stevens


Without changing the code, you can't do this. You're essentially talking about p.super.m1() which isn't 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 could create a facade over them and explicitly manage the state; this would work, but I wouldn't 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 a single good reason to do this in the real world. If you're using an OO language, don't fight the OO paradigm!

like image 32
Steve Chaloner Avatar answered Nov 20 '22 17:11

Steve Chaloner


I think it not possible. There are two ways to call a parent class method

1.) crate object of parent class as

Parent p = new Parent();

2.) Use super in child class method as

@Override
    public void m1() {
        super.m1();
        System.out.println("Child method");
    }
like image 3
java guy Avatar answered Nov 20 '22 16:11

java guy


Apart from the already mentioned way, you can declare both the methods as static.

so when you do this Parent p = new Child(); p.m1();

the static method of parent class would be called and the output will be "Parent method"

Note : The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves. So if you have a variable: private static int i = 0; and you increment it ( i++ ) in one instance, the change will be reflected in all instances.

like image 1
Nidhi Bhardwaj Avatar answered Nov 20 '22 17:11

Nidhi Bhardwaj