About Question enthuware.ocpjp.v8.2.1590 :
Posted: Fri Feb 10, 2017 9:22 pm
				
				why does the below run with no issues, where in CODE 1 A a = new B(); calls the method from A
and with CODE B Widget w = new GoodWidget(); the method is called from GoodWidget();
Thanks,
Kozy
CODE 1:
(Assume that assertions are enabled.)
CODE B:
What will the following code print when compiled and run?
			and with CODE B Widget w = new GoodWidget(); the method is called from GoodWidget();
Thanks,
Kozy
CODE 1:
Code: Select all
class A
{
   public void getItDone(int counter)
   {
      assert counter >= 0 : "Less than zero";
      for(int i=0; i<counter; i++){ }
   }
}
class B extends A
{
   public void getItDone(int counter)
   {
      assert counter < 100 : "Greater than 100";
      for(int i=counter; i>0; i--){ }
   }
   public static void main(String args[])
   {
      A a = new B();
      a.getItDone(-4);
   }
}
CODE B:
What will the following code print when compiled and run?
Code: Select all
abstract class Widget {
    String data = "data";
    public void doWidgetStuff() {
    }
}
class GoodWidget extends Widget{
    String data = "big data";
    public void doWidgetStuff() {
        System.out.println(data);
    }
}
public class WidgetUser{
    public static void main(String[] args) {
        Widget w = new GoodWidget();
        w.doWidgetStuff();
    }
   
}