inheritance in java.how to change the variable value of sub class by super class methods -


class sup {     private int i; //private one,not gonna inherited.     void seti(int s) //this set i.but going set becz in child class?     {     i=s;     system.out.println(i+"of sup class"); //to verify changed      }  }  class cid extends sup //this child class {     private int i; //this 2nd i. want change isnt changing on call seti method     void changi(int h) //this 1 working in changing 2nd i.     {         i=h;     }     void showci()     {      system.out.println(i+"of cid class");        } }  class test {     public static void main(string[] args)     {          cid ob= new cid();         ob.seti(3); //to set of cid class sets sup class         ob.showci(); //result shows nothing changed cid class         ob.changi(6); // works wanted         ob.showci(); // can changed of cid class      }  } 

please clarify me whenever use inheritance(or extends) fields(variables , methods except private ones) copy child(or sub)classes or fields can accessed child class?

with reference question here got access the private variable "i" when extended sup class got seti() method sup class sets value of var "i" in super class if override seti() method in cid class able change value of in subclass:

in case need use

sup s = new cid();  s.seti(10); // change value of in subclass class 


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -