Question: Examine the following piece of code example.java, and write out the output of the program when executed. Answer the following questions: class A { int

Examine the following piece of code "example.java", and write out the output of the program when executed. Answer the following questions:

 class A { 
 int i=0; void f(){ i=1; }; private void g(){ i=11; }; A() { f(); } A(boolean x) { g(); } 

}

 class B extends A { void f(){ i=9; }; public void g(){ i=99; }; public int h() { return i;} B() { super(); } B(boolean x) { super(x); } 
 public static void main(String[] args) { System.out.println( new B().h() ); 

} System.out.println( new B(true).h() ); }

1) What new file(s) would be generated if you compile example.java?

javac example.java

2) What are the constructors of class B? Are they overloaded or overridden?

3) Run the program by

java B

Did you get the same result as you had anticipated? What happens if you try to run

java A

Can you explain the result?

4) If f( ) of A is defined as private method, what results the program will produce? Can you explain why?

5) If you place the keyword public right in front of the definition of the class B so that you have

 public class B { .... 

can you still compile it by executing javac example.java? If not, explain why.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!