Question

class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

class Upper { private int i;

private String name; public Upper(int i){ name = "Upper";

this.i = i;}

public void set(Upper n){ i = n.show();} public int show(){return i;}

}

class Middle extends Upper { private int j;

private String name; public Middle(int i){

super(i+1);

name = "Middle"; this.j = i;}

public void set(Upper n){ j = n.show();} public int show(){return j;}

}

class Lower extends Middle { private int i;

private String name; public Lower(int i){

super(i+1); name = "Lower"; this.i = i;}

public void set(Lower n){ i = n.show();} public int show(){return i;}

}

class Tester {

public static void main(String[] args){ Lower a = new Lower(1);

Middle b = a;

Upper c = new Middle(5);

System.out.println(a.show()); System.out.println(b.show()); System.out.println(c.show());

a.set(c);

b.set(a);

c.set(b);

System.out.println(a.show()); System.out.println(b.show()); System.out.println(c.show());

}

}

  1. What is printed as a result of the first occurrence of System.out.println(a.show());? Answer is 1
  2. What is printed as a result of the first occurrence of System.out.println(b.show()); Answer is 1
  3. What is printed as a result of the first occurrence of System.out.println(c.show()); Answer is 5
  4. What is printed as a result of the second occurrence of System.out.println(a.show()); after the set commands? Answer is 1
  5. What is printed as a result of the second occurrence of System.out.println(b.show()); after the set commands? Answer is 1
  6. What is printed as a result of the second occurrence of System.out.println(c.show()); after the set commands? Answer is 1

Answers of the questions are given but i am not understanding the reason behind. I would appretiate if you explain in detail.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1 What is printed as a result of the first occurrence of System.out.println(a.show());? Answer is 1

This statement will call the method in show() in Lower class so it will return 1 as we passed 1 as argument while creating the object

2 What is printed as a result of the first occurrence of System.out.println(b.show()); Answer is 1

This statement will call the method in show() in Lower class so it will return 1 as we passed 1 as argument while creating the object

3 What is printed as a result of the first occurrence of System.out.println(c.show()); Answer is 5

This statement will call the method in show() in Upper class so it will return 5  as we passed 5 as argument while creating the Upper object

after calling the setters we passed the objects and it sets the values to 1

so after that it will print all 1

Add a comment
Know the answer?
Add Answer to:
class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT