vendredi 13 janvier 2017

child instance parameters passing in parent class

I have parent class :

class Parent {

  public void some() {
     return 10;
  }

    public String LAST_UPDATED_KEY = "parent_updated";
        public String getLastUpdated(String key) {
         // do some stuff
         System.out.println(key);  // prints parent_updated
         System.out.println(this.some())  // prints 5

        }

        public String getLastUpdated() {
            return this.getLastUpdated(this.LAST_UPDATED_KEY);
        }
    }

here is child class :

class Child extends Parent {
public String LAST_UPDATED_KEY = "child_updated";
  // some stuff
  public void some() {
     return 5;
  }
}

I am calling it using reflection as :

public void somemethod() {
        Class<? extends Parent> daoClass = (Class<? extends Parent>)Class.forName(Child.class);
        Method getInstance = daoClass.getMethod("getInstance");
 Parent child = getInstance.invoke(null);
 child.getLastUpdated(child.LAST_UPDATED_KEY) 

}

i did't understand why it prints parent's last updated key not child's and it do call some() method on child . Please explain behaviour. Thanks





Aucun commentaire:

Enregistrer un commentaire