Solved

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

Posted on 2009-07-06
6
226 Views
Last Modified: 2013-11-23
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";
      }
}
0
Comment
Question by:SunScreenCert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Accepted Solution

by:
fsze88 earned 200 total points
ID: 24790662
In statement
for(A aa: a){
                  System.out.println(aa.runNow() + " ");
}

That's for loop looking for parent class A from class array a[]
in statement System.out.println(aa.runNow() + " ");
All of aa are running  parent class A function runNow() , so return "High" only
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24791300
so do you mean to say that the foreach loop is not suitable for polymorphic call
0
 
LVL 8

Assisted Solution

by:WhiteMage
WhiteMage earned 200 total points
ID: 24791411
I believe it's because the method runNow() is declared as private in class A, and class B is static, and class C is outside of class A.  There are two solutions to this (choose one of these):

1) Either declare runNow() as protected or public (this is the easiest and probably what you wanted) instead of private like you did.  You cannot override a private method and expect "A a = new B();" to work.  You would need to do "B b = new B();".  (If it's private, it's really not overriding it; it knows nothing about it!)

2) Remove static from class B.  Put class C in class A.  Then you'll need to make two methods in class A to allow access.  (I'm not sure if this will work; I don't have a compiler with me and can't test this.)


I hope that works and helped.
//Example of solution 1:
package scjp;
 
public class A {
            //this is it
            public 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";
      }
}
 
 
//Example of solution 2:
package scjp;
 
public class A {
            private String runNow(){
                  return "High";
            }
      class B extends A{
            public String runNow(){
                  return "Low";
            }
      }
 
      class  C extends A.B{
           public String runNow(){
                 return "Out";
           }
      }
 
      public B createB() { return new B(); }
      public C createC() { return new C(); }
 
      public static void main(String[] args) {
            A t = new A();
            A a[] = new B[]{t.createB(), t.createC()};
            for(A aa: a){
                  System.out.println(aa.runNow() + " ");
            }
 
      }
 
}

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 200 total points
ID: 24792356
foreach loop is not suitable for polymorphic call?
you can try this , the result should be "Low" "High"

public static void main(String[] args) {
            B b[] = new B[]{new B(), new C()};
            for(B bb: b){
                  System.out.println(bb.runNow() + " ");
            }

      }
0
 
LVL 2

Assisted Solution

by:nkiggs
nkiggs earned 100 total points
ID: 24867720
I think fsze88's recent comment would override the base class' (class A) runNow() method with the extended class' (class B) runNow() method, hence it would produce "Low" "Low"

The trick is that the output is from the following "for" statement, which instantiates an object aa of class A that produces "High" and loops until the point when the statement a is true(ie. A aa: a) and executes the printing statement inside the for loop "High". Therefore the for loop body is only entered twice producing "High" "High":

for(A aa: a)
                  System.out.println(aa.runNow() + " ");
0
 
LVL 8

Assisted Solution

by:WhiteMage
WhiteMage earned 200 total points
ID: 24874791
No, it is suitable for polymorphism!  (And Java isn't true polymorphism like C++, it's more simulated, but that's another discussion.)

My 1st solution works.  Run it for yourself!  You only need to change runNow() in class A to be public or protected because you can't override a private method, since the derived children don't have access to it and don't even know it exists!  My 2nd solution doesn't work, and I'm not sure currently how to correct it, but it doesn't matter.  The solution (provided below) works!  I've run it, and I get "Low Out", like you would expect.  If runNow needs to be private, then I'm sure there is a way (you have to use non-static inner classes though.)
package scjp;
 
public class A {
            //EITHER USE PUBLIC OR PROTECTED HERE
            //This is the only thing I changed.
            public 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){
                  //Output is "Low\nOut"
                  System.out.println(aa.runNow() + " ");
            }
 
      }
 
}
class  C extends A.B{
      public String runNow(){
            return "Out";
      }
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 69
batch vs regular insert in spring DAO 2 53
Eclipse neon2 "Java build path" correctness 7 44
Java import explained 4 49
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question