Question

In: Computer Science

Given the following Java code: class C { public int foo(C p) { return 1; }...

Given the following Java code:

class C {
    public int foo(C p) { return 1; }
}

class D extends C {
    public int foo(C p) { return 2; }
    public int foo(D p) { return 3; }
}

C p = new C();
C q = new D();
D r = new D();

int i = p.foo(r);
int j = q.foo(q);
int k = q.foo(r);

(Remember that in Java every object is accessed through a pointer and that methods are virtual by default.)

Which methods get called in the three calls, i.e., what are the values of i, j, and k? Explain how the method selection works to result in these values.

Solutions

Expert Solution

For this statement, C p = new C(); , p object is created for class C as we have directly declared it.

Now for the second statement i.e., C q = new D(); ,since D is extending properties of C using inheritance, the object is created for class D i.e., q points to class D but of type class C.

Now in the similar way, the third statement i.e., D r = new D(); , here also the object is directly created to class D itself.

Now to compute i value in statement int i = p.foo(r); => p is object of class C and hence foo() in class C i.e.,

public int foo(C p) { return 1;  } gets invoked and value of 1 is returned.

Now to compute j value in statement int j=q.foo(q); => q is object of class D but type of class C and hence foo() in class D i.e.,

public int foo(C p) { return 2;  } gets invoked and value of 2 is returned.

Now to compute k value in statement int k=q.foo(r); => q is object of class D but type of class C and r is object of class D and hence foo() in class D i.e.,

public int foo(C p) { return 2;  } gets invoked and value of 2 is returned.

This is happening because of method overriding.

So the output will be

i=1

j=2

k=2


Related Solutions

1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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”);...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public...
I needv pseudocode and a flowchart for the following java code public class AcmePay { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int hours, shift, retirement = 0; do { System.out.print("Enter the number of hours worked (>0): "); hours = scanner.nextInt(); } while (hours <= 0); do { System.out.print("Enter shift [1 2 or 3]: "); shift = scanner.nextInt(); } while (shift < 1 || shift > 3); if (shift == 2 || shift ==...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT