waltermilner

Untitled

Nov 25th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. public class Test {
  2. // inheritance of fields
  3.   public static void main(String[] args) {
  4.     Sub ref=new Sub();
  5.     ref.method();
  6.    
  7.   }
  8.  
  9. }
  10.  
  11. class Base
  12. {
  13.   String val1="abc";
  14.   String val2="def";
  15. }
  16.  
  17. class Sub extends Base
  18. {
  19.   String val2="ghi"; // do not do this!!! val2 hides super val2
  20.   void method()
  21.   {
  22.     System.out.println(val1); // abc
  23.     System.out.println(val2); // ghi
  24.     System.out.println(super.val2); // def
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment