Link to home
Start Free TrialLog in
Avatar of SunScreenCert
SunScreenCert

asked on

The output of this program is "High" "High". Plz explain?

package scjp;

      public class A {
            private String runNow(){
                  return "High";
            }
      static class B extends A{
            public String runNow(){
                  return "Low";
            }
      }
      public static void main(String[] args) {
            A a[] = new B[]{new B(), new C()};
            for(A aa: a){
                  System.out.println(aa.runNow() + " ");
            }

      }

}
class  C extends A.B{
      public String runNow(){
            return "Out";
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SunScreenCert
SunScreenCert

ASKER

so do you mean to say that the foreach loop is not suitable for polymorphic call
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial