Facebook
From Abrupt Pintail, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 189
  1. What will the following code print when run?
  2.  
  3. class Baap {
  4.     public int h = 4;
  5.     public int getH() {
  6.         System.out.println("Baap " + h);
  7.         return h;
  8.     }
  9. }
  10.  
  11. public class Beta extends Baap {
  12.     public int h = 44;
  13.     public int getH() {
  14.         System.out.println("Beta " + h);
  15.         return h;
  16.     }
  17.     public static void main(String[] args) {
  18.         Baap b = new Beta();
  19.         System.out.println(b.h + " " + b.getH());
  20.         Beta bb = (Beta) b;
  21.         System.out.println(bb.h + " " + bb.getH());
  22.     }
  23. }