Ask Question
31 January, 09:15

Analyze the following code:public class Test {public static void main (String[] args) {new B (); }}class A {int i = 7; public A () {setI (20); System. out. println ("i from A is " + i); }public void setI (int i) {this. i = 2 * i; }}class B extends A {public B () {/ / System. out. println ("i from B is " + i); }public void setI (int i) {this. i = 3 * i; }}A) The constructor of class A is not called. B) The constructor of class A is called and it displays "i from A is 60". C) The constructor of class A is called and it displays "i from A is 40". D) The constructor of class A is called and it displays "i from A is 7".

+2
Answers (1)
  1. 31 January, 12:49
    0
    The answer is "Option B".

    Explanation:

    In the given java program the main class test is defined. Inside the class, class b instance is created. In the next line, two-class are defined that is class A and class B and class B inherit class A.

    In both classes we define a constructor and a method setI (). This method calculates the value that is passed in function and This class uses Overriding concept. In the given question other options are wrong that can be described as:

    In option A, Class B inherits class A and when class b object is created it is automatically called. In option C and option D, The constructor of class A is called but it displays 60, because of the constructor A call a function, that is setI () and passes value 20 parameter and because of using method overriding it display 60.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Analyze the following code:public class Test {public static void main (String[] args) {new B (); }}class A {int i = 7; public A () {setI ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers