1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class A { int i = 10; public void add(){ System.out.println("add function in Class A is running..."); i = i + 2; System.out.print("i = " + i); } public static void main(String []args) { A a = new B(); System.out.println("before running function add: a.i = " + a.i); a.add(); System.out.println("after running function add: a.i = " + a.i); } }
class B extends A{ int i = 20; public void add(){ System.out.println("add function in Class B is running..."); i = i + 3; System.out.print("i = "+i); } }
|